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
WBOYOriginal
2016-05-16 16:14:121323browse

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 = /^-&#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> 

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.

Statement:
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