I am adding two numbers, but I don't get a correct value.(我将两个数字相加,但得不到正确的值。)
1 + 2
function myFunction() { var y = document.getElementById("txt1").value; var z = document.getElementById("txt2").value; var x = y + 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>
They are actually strings, not numbers.(它们实际上是字符串,而不是数字。)
+
var x = +y + +z;
1.4m articles
1.4m replys
5 comments
57.0k users