Adding two numbers concatenates them instead of ca

2018-12-31 05:33发布

I am adding two numbers, I get a correct value.

Got the value after sum

function myFunction() {
  var y = document.getElementById("txt1").value;
  var z = document.getElementById("txt2").value;
  var x = Number(y) + Number(z);
  document.getElementById("demo").innerHTML = x;
}
<p>
  Click the button to calculate x.
  <button onclick="myFunction()">Try it</button>
</p>
<p>
  Enter first number:
  <input type="text" id="txt1" name="text1" value="1">
  Enter second number:
  <input type="text" id="txt2" name="text2" value="2">
</p>
<p id="demo"></p>

20条回答
宁负流年不负卿
2楼-- · 2018-12-31 06:24

Use parseFloat it will convert string to number including decimal values.

 function myFunction() {
      var y = document.getElementById("txt1").value;
      var z = document.getElementById("txt2").value;
      var x = parseFloat(y) + parseFloat(z);
      document.getElementById("demo").innerHTML = x;
    }


<p>
  Click the button to calculate x.
  <button onclick="myFunction()">Try it</button>
</p>
<p>
  Enter first number:
  <input type="text" id="txt1" name="text1" value="1">
  Enter second number:
  <input type="text" id="txt2" name="text2" value="2">
</p>
<p id="demo"></p>
查看更多
刘海飞了
3楼-- · 2018-12-31 06:25

This code sums both the variables! Put it into your function

var y = parseInt(document.getElementById("txt1").value);
var z = parseInt(document.getElementById("txt2").value);
var x = (y +z);
document.getElementById("demo").innerHTML = x;`
查看更多
时光乱了年华
4楼-- · 2018-12-31 06:25

You can do a precheck with regular expression wheather they are numbers as like

function myFunction() {
    var y = document.getElementById("txt1").value;
    var z = document.getElementById("txt2").value;
    if((x.search(/[^0-9]/g) != -1)&&(y.search(/[^0-9]/g) != -1))
      var x = Number(y)+ Number(z);
    else
      alert("invalid values....");
    document.getElementById("demo").innerHTML = x;
  }
查看更多
不流泪的眼
5楼-- · 2018-12-31 06:29

boss just add a simple type casting method as the input is taken in text use following

    var y = parseInt(document.getElementById("txt1").value);
    var z = parseInt(document.getElementById("txt2").value);
    var x = y + z;
查看更多
人气声优
6楼-- · 2018-12-31 06:30

try this

<!DOCTYPE html>
<html>
<body>

<p> Add Section </p>

<label>First No :</label>
<input id="txt1"  type="text"/><br />
<label>Second No :</label>
<input id="txt2"  type="text"/><br />

<input type="button" name="Add" value="Add" onclick="addTwoNumber()"/>
<p id="demo"></p>
<script>
function myFunction() {
    document.getElementById("demo").innerHTML = Date();
}

function addTwoNumber(){
    var a = document.getElementById("txt1").value;
    var b = document.getElementById("txt2").value;

    var x = Number(a) + Number(b);
    document.getElementById("demo").innerHTML = "Add Value: " + x;
}
</script>

</body>
</html> 
查看更多
临风纵饮
7楼-- · 2018-12-31 06:35

Simple

var result = parseInt("1") + parseInt("2");
console.log(result ); // Outputs 3
查看更多
登录 后发表回答