Javascript Jquery—克隆元素的总和';不能使用来自函数的输入

Javascript Jquery—克隆元素的总和';不能使用来自函数的输入,javascript,jquery,input,Javascript,Jquery,Input,这是我的密码: $(document).ready(function() { $('#clone').click(function() { var target = $(this).closest('.groupcontainer'); target.clone(true, true).insertAfter(target); }); $('.input').on('input',function(){ $(this).parent().children('

这是我的密码:

$(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
中,则
小计将更新。有人能解释一下我错过了什么吗

这是我的建议。它看起来有点马虎,但它给出了一个想法

数量*系统将自动更新我的
.grouptotal

如何使
#subtotal
.grouptotal
自动相加?我尝试将('change'、'.grouptotal',function(){
“change”改为'input'和其他几个,但没有成功


此外,如果单击
#clone
按钮复制组,则该功能必须工作。

当使用
val('value')
以编程方式设置值时,
未触发
更改
事件,您必须实际触发它

$(this).parent().children('.grouptotal').val( $(this).val() ).trigger('change')

让我们再试一次。显然,以编程方式更改grouptotal值不会触发更改事件。我本以为会触发更改事件,因此您的代码在我看来是正确的。但是,为了修复此问题,我添加了一个显式触发器“.grouptotal”。您可以看到

只需添加代码:

('.grouptotal').change();

这可能不是您要寻找的解决方案,但它似乎有效

我明白了,但可能我做得不对?现在我的数量*系统价格没有在grouptotal中更新。
$('.input').on('input',function(){$(this).parent().children('.grouptotal').val($(this).val().trigger('change')*$(this.parent().children('.input2').val();});$('.input2').on('input',function(){$(this.parent().children('.grouptotal').val('this.val().trigger('change')*$(this.parent().children('.input').val());););
这起作用了,玩了它。像这样添加了.trigger:
$(this.parent().children)('.grouptotal').trigger('change')
。谢谢!谢谢,但是当我去掉+时,它没有添加,它只是将
。grouptotal
s串联起来,而不是对它们进行汇总。对我来说似乎很好,请查看小提琴:对不起-我误解了你的问题-我明白了。让我重新阅读!太好了!很高兴能提供帮助!