Home > Web Front-end > JS Tutorial > Implementation method of summing multiple inputs in jQuery_jquery

Implementation method of summing multiple inputs in jQuery_jquery

WBOY
Release: 2016-05-16 16:14:12
Original
1326 people have browsed it

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> 
Copy after login

Part of the jQuery code is as follows:

<script> 
$("input[id^='add']").change(function(){ 
    var sum=0; 
    $("input[id^='add']").each(function(){ 
      var r = /^-&#63;\d+$/ ; //正整数 
      if($(this).val() !=''&&!r.test($(this).val())){ 
       $(this).val("");  //正则表达式不匹配置空 
      }else if($(this).val() !=''){ 
       sum+=parseInt($(this).val()); 
      } 
      document.getElementById("add").value=sum; 
      }); 
    }); 
</script> 

Copy after login

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.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template