[英]Jquery - Sum of cloned elements won't work with input from function
这是我的代码:
$(document).ready(function() {
$('#clone').click(function() {
var target = $(this).closest('.groupcontainer');
target.clone(true, true).insertAfter(target);
});
$('.input').on('input',function(){
$(this).parent().children('.grouptotal').val($(this).val() * $(this).parent().children('.input2').val());
});
$('.input2').on('input', function(){
$(this).parent().children('.grouptotal').val($(this).val() * $(this).parent().children('.input').val());
});
});
$(document).on('change', '.grouptotal', function(){
var sum = 0;
$('.grouptotal').each(function(){
sum += +$(this).val();
});
$('#subtotal').val(sum);
});
我的问题是,我需要#subtotal
添加的每个实例.grouptotal
,但#subtotal
当它到达它的输入来自未更新
$('.input').on('input',function(){
$(this).parent().children('.grouptotal').val($(this).val() *
$(this).parent().children('.input2').val());
});
$('.input2').on('input', function(){
$(this).parent().children('.grouptotal').val($(this).val() *
$(this).parent().children('.input').val());
});
如果我将数字手动放在.grouptotal
则#subtotal
将更新。 有人可以向我解释我所缺少的吗?
这是小提琴 。 它看起来有些草率,但是却给出了主意。
数量*系统将自动更新我的.grouptotal
如何使#subtotal
接受.grouptotal
并自动添加它们? 我试过将$(document).on('change', '.grouptotal', function(){
“ change”更改为'input'和其他几个,但是没有运气。
同样,如果单击#clone
按钮以复制组,则它必须能够正常工作。
使用val('value')
以编程方式设置值时,不会触发change
事件,您必须实际触发它
$(this).parent().children('.grouptotal').val( $(this).val() ).trigger('change')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.