在两个输入框中输入数字,例如8和3,乘法可以正常运算,输出24,而加法就会输出83.代码如下:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script language="javascript">
window.onload = function math(){
x = document.form1.fvalue;
i = document.form1.svalue;
z = document.getElementById("link");
z.onclick = function(){
alert(x.value+i.value);
}
}
</script>
</head><body>
<form name="form1">
<input type="text" name="fvalue" size="10" />+<input type="text" name="svalue" size="10" />=
    <a id="link">结果</a>
</form>
</body>
</html>

解决方案 »

  1.   

    alert(parseInt(x.value)+parseInt(i.value));
      

  2.   


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    <script language="javascript">
    window.onload = function math(){
        x = document.form1.fvalue;
        i = document.form1.svalue;
        z = document.getElementById("link");
        //取到的value是字符串类型,乘法时会自动转换为字符型,但加法时 直接就是字符串相加了
        z.onclick = function(){
            alert(parseFloat(x.value, 10) + parseFloat(i.value, 10));
        }
    }
    </script>
    </head><body>
    <form name="form1">
        <input type="text" name="fvalue" size="10" />+<input type="text" name="svalue" size="10" />=
        <a id="link">结果</a>
    </form>
    </body>
    </html>