更改选择器值的jQuery函数 [英] jQuery function on changing selector's value

查看:59
本文介绍了更改选择器值的jQuery函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自动求和的值的函数b $ b 用户输入 为小计
输入   : 

 function calculate(child,parent){
var sum = 0;
$(child).not(this).each(function()
if(!isNaN(this.value)&& this.value.length!= 0){
sum + = parseFloat(this.value);
}
});
$(parent).val(sum.toFixed(2));
}




 

总计< input type =" text" ID = QUOT; P01" > < / BR>

& nbsp;小计1< input type =" text" ID = QUOT; P0101"平变化= QUOT;计算(" [ID ^ = P010]","#P01’ )" >< / BR>

& nbsp; &安培; NBSP; child11< input type =" text" ID = QUOT; c010101"的onkeyup = QUOT;计算(" [ID ^ = C0101]","#P0101’ )" >< / BR>

& nbsp; &安培; NBSP; child12< input type =" text" ID = QUOT; c010102"的onkeyup = QUOT;计算(" [ID ^ = C0101]","#P0101’ );" >< / BR>

& nbsp;小计2< input type =" text" ID = QUOT; P0102"平变化= QUOT;计算(" [ID ^ = P010]","#P01’ )" value =" 0"> < / br>

< span style ="font-family:monospace; font-size:11.9925px; white-space:pre">& nbsp; &安培; NBSP; child21< input type =" text" ID = QUOT; c010201"的onkeyup = QUOT;计算(" [ID ^ = c0102]","#P0102’ );" >

如果他们的值自动变化,我如何将小计总和加入总数?我尝试使用onchange事件但不起作用。



解决方案



onchange 事件发生在内容更改后,元素失去焦点。使用onchange进行小计,你需要对小计进行更改,然后执行脚本。


要对小计进行求和,可以通过childs onkeyup事件触发计算功能。

< script src =" http://code.jquery.com/jquery-1.11.3.min.js"类型= QUOT;文本/ JavaScript的">< /脚本> 

< script>

函数calculate(子,父){

var sum = 0;


(child).each(function(){

if(!isNaN(this.value)&& this.value.length!= 0){

sum + = parseFloat(this.value);

}

});


(parent).val(sum.toFixed(2));

}

< / script>

总计< input type =" text" ID = QUOT; P01" > < / BR>

& nbsp;小计1< input type =" text" ID = QUOT; P0101" >< / BR>

& nbsp; &安培; NBSP; child11< input type =" text" ID = QUOT; c010101"的onkeyup = QUOT;计算(" [ID ^ = C0101]","#P0101’ );计算(" [ID ^ = P010]","#P01’ )" >< / BR>

& nbsp; &安培; NBSP; child12< input type =" text" ID = QUOT; c010102"的onkeyup = QUOT;计算(" [ID ^ = C0101]","#P0101’ );计算(" [ID ^ = P010]","#P01’ )" >< / BR>

& nbsp;小计2< input type =" text" ID = QUOT; P0102"值= QUOT; 0">< / BR>

& nbsp; &安培; NBSP; child21< input type =" text" ID = QUOT; c010201"的onkeyup = QUOT;计算(" [ID ^ = c0102]","#P0102’ );计算(" [ID ^ = P010]","#P01’ )" >





最好的问候,


Linda  Zhang


I have a function that sum automatically values of user inputs into subtotals inputs 

function calculate(child,parent) {
     var sum = 0;
  $(child).not(this).each(function() 
      if (!isNaN(this.value) && this.value.length != 0 ) {    		             
            sum += parseFloat(this.value);       
 	  	 }         
      });
  	$(parent).val(sum.toFixed(2)); 
}


Total<input type="text" id="p01" > </br> &nbsp; Subtotal1<input type="text" id="p0101" onchange="calculate('[id^=p010]','#p01')" ></br> &nbsp; &nbsp; child11<input type="text" id="c010101" onkeyup="calculate('[id^=c0101]','#p0101')" ></br> &nbsp; &nbsp; child12<input type="text" id="c010102" onkeyup="calculate('[id^=c0101]','#p0101');" ></br> &nbsp; Subtotal2<input type="text" id="p0102" onchange="calculate('[id^=p010]','#p01')" value="0"></br>

&nbsp; &nbsp; child21<input type="text" id="c010201" onkeyup="calculate('[id^=c0102]','#p0102');" >

How can I do to sum subtotals into the total when their values are changing automatically? I tried with onchange event but doesn't work.


解决方案

Hi,

The onchange event occurs when the element loses focus, after the content has been changed. Using the onchange for subtotals, you need to make changes on subtotals, then the script will be executed.

To sum subtotals, you can trigger the calculate function by childs onkeyup event.

<script src="http://code.jquery.com/jquery-1.11.3.min.js" type="text/javascript"></script>

<script>

function calculate(child,parent) {

     var sum = 0;


(child).each(function(){ if (!isNaN(this.value) && this.value.length != 0 ) { sum += parseFloat(this.value); } });


(parent).val(sum.toFixed(2)); } </script> Total<input type="text" id="p01" > </br> &nbsp; Subtotal1<input type="text" id="p0101" ></br> &nbsp; &nbsp; child11<input type="text" id="c010101" onkeyup="calculate('[id^=c0101]','#p0101');calculate('[id^=p010]','#p01')" ></br> &nbsp; &nbsp; child12<input type="text" id="c010102" onkeyup="calculate('[id^=c0101]','#p0101');calculate('[id^=p010]','#p01')" ></br> &nbsp; Subtotal2<input type="text" id="p0102" value="0"></br> &nbsp; &nbsp; child21<input type="text" id="c010201" onkeyup="calculate('[id^=c0102]','#p0102');calculate('[id^=p010]','#p01')" >


Best regards,

Linda Zhang


这篇关于更改选择器值的jQuery函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆