日期:2014-05-20 浏览次数:20520 次
function aaa(form) {var a=0; var b=0; var c=0; var sum=0; a=parseInt(form.aa.value); b=parseInt(form.bb.value); if(a>b){c=b;b=a;a=c;} for(;a<=b;a++) { sum+=a*a; } form.answer.value=sum; }
------解决方案--------------------
input 获取的值JS默认按照 字符处理应该转换为数字:
a=parseInt(form.aa.value);
b=parseInt(form.bb.value);
------解决方案--------------------
a=parseInt(form.aa.value);
b=parseInt(form.bb.value);
form.answer.value=(a*a + b*b)
------解决方案--------------------
<!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> <title>两个数的平方和</title> <script src="Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script> <script type="text/javascript"> $(function () { $("#btnGetVal").click(function () { var valueA = parseInt($("input[name='aa']").val()); var valueB = parseInt($("input[name='bb']").val()); $("input[name='answer']").val(valueA * valueA + valueB * valueB); }); }); </script> </head> <body> <form action="" method="post"> <h1>两个数的平方和</h1> 第一个数<input type="text" name="aa" /><br/> 第二个数<input type="text" name="bb" /><br/> <input name="button" type="button" id="btnGetVal" value="两个数的平方和"/><br/> <input type="text" name="answer" /><br/> </form> </body> </html>