[英]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.