javascript - Addition operation issues? - Stack Overflow

Hi i'm trying to do simple addition of two numbers in javascript. When i'm trying to get the

Hi i'm trying to do simple addition of two numbers in javascript. When i'm trying to get the two input element values, the result is ing in a concatenation of two numbers

Here is the code:

<html>
<title>
</title>
<head>
<script type="text/javascript">
function loggedUser() {
    //Get GUID of logged user
   //alert('success');
   var x, y , result;
   x = document.getElementById('value1').value;
   y = document.getElementById('value2').value;
   result=x+y;
   alert(result);
   document.getElementById('res').value = result;
}
</script>
</head>
<body>
<input type="text" id="value1"><br>
<input type="text" id="value2"><br>
<input type="text" id="res">
<input type="submit" value ="submit" onclick=loggedUser();>
</body>
</html>

Hi i'm trying to do simple addition of two numbers in javascript. When i'm trying to get the two input element values, the result is ing in a concatenation of two numbers

Here is the code:

<html>
<title>
</title>
<head>
<script type="text/javascript">
function loggedUser() {
    //Get GUID of logged user
   //alert('success');
   var x, y , result;
   x = document.getElementById('value1').value;
   y = document.getElementById('value2').value;
   result=x+y;
   alert(result);
   document.getElementById('res').value = result;
}
</script>
</head>
<body>
<input type="text" id="value1"><br>
<input type="text" id="value2"><br>
<input type="text" id="res">
<input type="submit" value ="submit" onclick=loggedUser();>
</body>
</html>
Share Improve this question edited Jul 27, 2011 at 5:03 Cameron Skinner 54.7k3 gold badges69 silver badges86 bronze badges asked Jul 27, 2011 at 5:01 Siva Siva 3,7488 gold badges29 silver badges28 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 4

The "+" operator is overloaded. If any of the parameters is a string, they are all converted to strings and concatenated. If the parameters are numbers, then addition is done. Form control values are always strings.

Convert the parameters to numbers first using one of the following:

x = Number(document.getElementById('value1').value);

or

x = parseInt(document.getElementById('value1').value, 10);

or

x = parsefloat(document.getElementById('value1').value);

or

x = +document.getElementById('value1').value;

or

x = document.getElementById('value1').value * 1;

and so on...

Oh, you can also convert it only when necessary:

result = Number(x) + Number(y);

etc.

The input fields contain strings. If you want to sum two numbers, you have to convert the strings to numbers before adding - otherwise you are just adding two strings. There are lots of ways to do that depending upon what you want to allow. Here's one:

   var x, y , result;
   x = Number(document.getElementById('value1').value);
   y = Number(document.getElementById('value2').value);
   result=x+y;
   alert(result);
   document.getElementById('res').value = result;

See it working here: http://jsfiddle/jfriend00/BWW2R/

document.getElementById('').value returns a string. You need to call parseInt on that to make it a number.

It's because x and y are actually strings, not numbers. The value field of the element is always a string, so x+y gives you the concatenation of x and y.

You can parse x and y as integers and add the result of the parsing: that will give you what you want.

The problem is when you take those values you are getting a string and in result you are doing a concatenation. You should use parseInt on both x and y like this:

x = parseInt(document.getElementById('value1').value);
y = parseInt(document.getElementById('value2').value);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745266560a4619492.html

相关推荐

  • javascript - Addition operation issues? - Stack Overflow

    Hi i'm trying to do simple addition of two numbers in javascript. When i'm trying to get the

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信