简体   繁体   English

计算两个值之和的函数中的JavaScript错误

[英]JavaScript error in function which calcs sum of two values

this is my JS code to check the match of two digits 这是我的JS代码,用于检查两位数的匹配情况

<script type="text/javascript">
function match(){
var a=parseInt(document.getElementById("one"));
var b=parseInt(document.getElementById("two"));
var c=parseInt(document.getElementById("sum"));
var d;
d=a+b;
if(d!=c)
    {
    alert("Something is wrong!!");
    }
    else
        {
            alert ("Success");

        }

}
</script>

html code for generat two digits and check on the process. 生成两位数的html代码,然后检查该过程。

<form action="#" method="post" onsubmit="return match();">
<input type="text"  name="one" id="one" value="<?php echo rand(1,9); ?>" readonly="readonly"/>
+
<input type="text" name="two" id="two" value="<?php echo rand(1,9); ?>" readonly="readonly"/>
=
<input type="text" name="sum" id="sum" />
<input type="submit" value="submit" />
</form>

Perhaps you want their value and not the element itself. 也许您想要它们的值,而不是元素本身。

var a=parseInt(document.getElementById("one").value,10);
var b=parseInt(document.getElementById("two").value,10);
var c=parseInt(document.getElementById("sum").value,10);

Also, provide the radix when you are using parseInt() otherwise it may go crazy sometimes. 另外,在使用parseInt()时提供radix ,否则有时可能会发疯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM