Home >Web Front-end >JS Tutorial >Implementation method of summing multiple inputs in jQuery_jquery
The example in this article describes the implementation method of summing multiple inputs in jQuery. Share it with everyone for your reference. The specific implementation method is as follows:
The html page code is as follows:
<td> <input name="add" id="add" readonly="readonly"/> </td> <pre name="code" class="html"><td> <input name="add1" id="add1"/> </td> <td> <input name="add2" id="add2"/> </td>
Part of the jQuery code is as follows:
<script> $("input[id^='add']").change(function(){ var sum=0; $("input[id^='add']").each(function(){ var r = /^-?\d+$/ ; //正整数 if($(this).val() !=''&&!r.test($(this).val())){ $(this).val(""); //正则表达式不匹配置空 }else if($(this).val() !=''){ sum+=parseInt($(this).val()); } document.getElementById("add").value=sum; }); }); </script>
Since the input attribute is readonly, when you press Backspace to delete the value of the input in the browser, the page will return. For the solution, please refer to the previous article "JQuery Implementation Method to Prevent the Backspace Key from Returning》
I hope this article will be helpful to everyone’s jQuery programming.