2013-08-30 2 views
0

임 각 입력 행에 입력 val()을 계산하려고하는데 현재는 1 행만 계산하면 도움이됩니다. 감사합니다!새로 생성 된 각 행의 jQuery 합

HTML

<a href="#" class="new">new</a> 
<ul class="container"></ul> 
<span class="all"></span> 

JS

$('.new').click(function(){ 
    $("ul").append('<li><input type="text" class="inputA"/><input type="text" class="inputB"/><p>total:<span></span></p></li>'); 
}); 

$('.container').on('keyup', 'input', function(){ 

    var a = 0; 
    var b = 0; 
    var c = 0; 
    var d = 0; 

    $('ul li').each(function(){ 
    $(this).parent().find('li input').each(function(){ 
     a = parseInt($('.inputA').val()); 
     b = parseInt($('.inputB').val()); 
     c = a+b; 
     $(this).parent().find('p span').html(c); 
    }); 
    d += c; 
    }); 

    $('.all').html(d); 

}); 

FIDDLE

+1

는 처음으로'.inputA'와'.inputB'를 따기 있기 때문에 그것은 아마. –

+0

그 문제 외에도'$ ('ul li'). 각각은'$ (this) .children ('li')이되어야합니다. 각각 (' – Blazemonger

답변

1

사용

$('.new').click(function() { 
    $("ul").append('<li><input type="text" class="inputA"/><input type="text" class="inputB"/><p>total:<span></span></p></li>'); 
}); 

$('.container').on('keyup', 'input', function() { 

    var d = 0; 
    $('.container li').each(function() { 
     var $this = $(this), 
      a = parseFloat($this.find('.inputA').val()) || 0, 
      b = parseFloat($this.find('.inputB').val()) || 0 
     var subTotal = a + b; 
     $this.find('span').text(subTotal); 
     d += subTotal; 
    }) 

    $('.all').html(d); 

}); 

데모 :

$('.new').click(function() { 
    $("ul").append($('<li>', { 
     html: [ 
      $('<input>', {class: 'inputA'}), 
      $('<input>', {class: 'inputB'}), 
      $('<span>', {class: 'total'}) 
     ] 
    }).data('total', 0)); //set list item total to ZERO 
}); 

$('.container').on('keyup', 'input', function() { 
    var other_input = $(this).siblings('input'); 
    var totalEl = $(this).siblings('.total'); 
    var total = parseInt(this.value); 
    var list_item = $(this).parents('li'); 
    total += parseInt(other_input.val()); 
    totalEl.text(total); //show new total 
    list_item.data('total', total); //set list item's total 

    var real_total = 0; 
    var allEl = $('.all'); 
    list_item.parent().find('li').each(function(){ 
     var t = $(this).data('total'); 
     real_total += t; 
    }); //tally up all list item totals 
    allEl.text(real_total); //display the total total 
}); 

바이올린 : 여기 Fiddle

+0

모두에게 감사드립니다. –

+0

호기심에서, $ this = $ (this)'? –

+0

또한 쉼표로 구분 된 구문이 어떻게 작동합니까? 장점은 무엇입니까? –

1
$('.new').click(function() { 
    $("ul").append('<li><input type="text" class="inputA"/><input type="text" class="inputB"/><p>total:<span></span></p></li>'); 
}); 

$('.container').on('keyup', 'input', function() { 
    var total = 0; 
    $('ul li').each(function() { 
     var a = parseInt($(this).find('.inputA').val(), 10) || 0; 
     var b = parseInt($(this).find('.inputB').val(), 10) || 0; 
     $(this).find('p span').html(a + b); 
     total += (a + b); 
    }); 
    $('.all').html(total); 
}); 

jsFiddle example

0

간결한 버전. 보너스로 자동으로 새 라인을 처리하는 코드가 포함되어 있습니다.

$('.new').click(function() { 
    $("ul").append('<li><input type="text" class="inputA"/><input type="text" class="inputB"/><p>total:<span></span></p></li>'); 
}); 


$(document).on("blur","input.inputB",function() { 
    $('.new').click(); 
}); 

$('.container').on('keyup', 'input', function (e) { 

    //var code = e.keyCode || e.which; 

    var d = 0; 

    $('ul li').each(function() { 
     a = parseInt($(this).find('input.inputA').val()); 
     b = parseInt($(this).find('input.inputB').val()); 
     c = a + b; 
     $(this).find('p span').html(c); 
     d += c; 
    }); 


    $('#all').html(d); 

}); 

[jsFiddle]

관련 문제