JQ实现表格内数据内容的相加
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script type="text/javascript" src="js/JQ3.3.1.js"></script> <script type="text/javascript"> $(function(){ var first = $("#first");// 获得ID为first标签的jQuery对象 var second = $("#second");// 获得ID为first标签的jQuery对象 var sumSP = $("#sumSP");// 获得ID为first标签的jQuery对象 first.change(function(){//第一个数 var num1 = first.val();// 取得first对象的值 var num2 = second.val();// 取得second对象的值 var sum = add(num1,num2); sumSP.text(sum); }); second.change(function(){//第二个数 var num1 = first.val(); var num2 = second.val(); var sum = add(num1,num2); sumSP.text(sum); }); }); function add(arg1,arg2){ var a1,a2,m; try{ a1 = arg1.toString().split(".")[1].length }catch(e){ a1 = 0; } try{ a2 = arg2.toString().split(".")[1].length }catch(e){ a2 = 0; } m = Math.pow(10, Math.max(a1,a2)); return (arg1*m+arg2*m)/m; } </script> </head> <body> <center> <form action=""> 第一个数:<input type="text" size="25" id="first" /><br /><br /> 第二个数:<input type="text" size="25" id="second" /><br /><br /> 它们的和:<span id="sumSP" style="color:red"></span> </form> </center> </body> </html>