欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

jQuery多個input求和的實(shí)現(xiàn)方法

 更新時間:2015年02月12日 14:49:49   作者:LMJudas  
這篇文章主要介紹了jQuery多個input求和的實(shí)現(xiàn)方法,涉及jQuery獲取input表單元素值及運(yùn)算的相關(guān)技巧,需要的朋友可以參考下

本文實(shí)例講述了jQuery多個input求和的實(shí)現(xiàn)方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:

html頁面代碼如下:

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

jQuery部分代碼如下:

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

由于input屬性為readonly,所以在瀏覽器中按Backspace刪除該input的值時會出現(xiàn)頁面返回的情況,解決方法可參照前面一篇《JQuery實(shí)現(xiàn)防止退格鍵返回的方法

希望本文所述對大家的jQuery程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評論