2016-11-15 116 views
0

所以我有一個表格,其中用戶將輸入一些內容並在最後一列中,它將根據其他列的輸入自動填充。它工作得很好,但是一旦我在表中添加另一行,它不會像原始行那樣填充最後一列。jquery不能在動態添加的表格行上工作

所以我的問題是什麼是錯的,我該如何解決這個問題?

謝謝!

添加行腳本:

$('.add_row_count').on('click',function(){ 
event.preventDefault(); 
var newRow = $('<tr><td style = "text-align: center;"><input type="text" name="categ_name[]" id="categ_name" value="" placeholder="Item Category" required></td> ' + 
'<td style = "text-align: center;"><input type="number" name="beg_count[]" id="beg_count" value="" placeholder="BEG Count" required></td>' + 
'<td style = "text-align: center;"><input type="number" name="del_count[]" id="del_count" value="" placeholder="DEL Count" required></td>' + 
'<td style = "text-align: center;"><input type="number" name="ret_count[]" id="ret_count" value="" placeholder="RET Count" required></td>' + 
'<td style = "text-align: center;"><input type="number" name="exc_count[]" id="exc_count" value="" placeholder="EXC Count" required></td>' + 
'<td style = "text-align: center;"><input type="number" name="pout_count[]" id="pout_count" value="" placeholder="P-Out Count" required></td>' + 
'<td style = "text-align: center;"><input type="number" name="sales_count[]" id="sales_count" value="" placeholder="SALES Count" required></td>' + 
'<td style = "text-align: center;"><input type="number" name="end_count[]" id="end_count" value="" placeholder="END Count" required></td>' + 
'<td style = "text-align: center;"><input type="number" name="actual_count[]" id="actual_count" value="" placeholder="Actual Count" required></td>' + 
'<td style = "text-align: center;"><input type="number" name="variance[]" id="variance" value="" placeholder="Variance" readonly></td>' + 
'<td style="border-top: 1px solid white; border-bottom: 1px solid white; border-right: 1px solid white"><button class="del" id="delete_row_count" type="button" onclick="deleterow_count();">Delete row</button></td></tr>'); 
$('table.count').append(newRow); 
}); 

腳本用於填充最後一欄

 $('#categ_name,#beg_count, #del_count, #ret_count,#exc_count, #pout_count, #sales_count,#end_count, #actual_count, #variance').keyup(function(){ 

    var end = $('#end_count').val(); 
    var ac = $('#actual_count').val(); 

    var total_variance = end - ac; 


    $('#variance').val(total_variance);  
    }); 

小提琴:https://jsfiddle.net/qhk3ha75/3/

+1

其中一個原因可能是與你必須有唯一的ID名稱的事實。您不能添加具有相同IDID值的行。 – Rasclatt

+3

可能重複[事件綁定動態創建的元素?](http://stackoverflow.com/questions/203198/event-binding-on-dynamically-created-elements) – DelightedD0D

+1

複製代碼的問題,但總之,使用'$(' ('click','.add_row_count',function(){....'(除了將您的id更改爲類) – DelightedD0D

回答

0

你真正需要的是利用classes整個代碼和使用event delegation來收聽dynamic elements上的活動。

下面是一個例子的解決方案:

jsFiddle

var $tbody = $('#count tbody'); 
 
$('.add_row_count').on('click', function() { 
 
    event.preventDefault(); 
 
    var $row = $tbody.find('tr').eq(0).clone(); 
 
    $row.find('input').val(''); 
 
    var $newRow = $($row[0].outerHTML); 
 
    $tbody.append($newRow); 
 
}); 
 
$tbody.on('keyup', '.calculate', function() { 
 
    var $tr = $(this).closest('tr'); 
 
    var end = $tr.find('.end_count').val(); 
 
    var ac = $tr.find('.actual_count').val(); 
 
    var total_variance = end - ac; 
 
    $tr.find('.variance').val(total_variance); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<a href="" value="" class="add_row_count" id="hide">Add another row</a> 
 
<table id='count' class="table table-bordered count" cellspacing="0" width="100%"> 
 
    <thead> 
 
    <tr> 
 
     <th style="text-align: center; width:180px"></th> 
 
     <th style="text-align: center;">BEG</th> 
 
     <th style="text-align: center;"> DEL</th> 
 
     <th style="text-align: center;"> RET</th> 
 
     <th style="text-align: center;"> EXC</th> 
 
     <th style="text-align: center;"> P-OUT</th> 
 
     <th style="text-align: center;"> SALES</th> 
 
     <th style="text-align: center;"> END</th> 
 
     <th style="text-align: center;"> ACTUAL COUNT</th> 
 
     <th style="text-align: center;"> VARIANCE</th> 
 
    </tr> 
 
    </thead> 
 
    <tbody> 
 
    <tr> 
 
     <td style="text-align: center;"> 
 
     <input type="text" name="categ_name[]" class="calculate categ_name" value="" placeholder="Item Category" required> 
 
     </td> 
 
     <td style="text-align: center;"> 
 
     <input type="number" name="beg_count[]" class="calculate beg_count" value="" placeholder="BEG Count" required> 
 
     </td> 
 
     <td style="text-align: center;"> 
 
     <input type="number" name="del_count[]" class="calculate del_count" value="" placeholder="DEL Count" required> 
 
     </td> 
 
     <td style="text-align: center;"> 
 
     <input type="number" name="ret_count[]" class="calculate ret_count" value="" placeholder="RET Count" required> 
 
     </td> 
 
     <td style="text-align: center;"> 
 
     <input type="number" name="exc_count[]" class="calculate exc_count" value="" placeholder="EXC Count" required> 
 
     </td> 
 
     <td style="text-align: center;"> 
 
     <input type="number" name="pout_count[]" class="calculate pout_count" value="" placeholder="P-Out Count" required> 
 
     </td> 
 
     <td style="text-align: center;"> 
 
     <input type="number" name="sales_count[]" class="calculate sales_count" value="" placeholder="SALES Count" required> 
 
     </td> 
 
     <td style="text-align: center;"> 
 
     <input type="number" name="end_count[]" class="calculate end_count" value="" placeholder="END Count" required> 
 
     </td> 
 
     <td style="text-align: center;"> 
 
     <input type="number" name="actual_count[]" class="calculate actual_count" value="" placeholder="Actual Count" required> 
 
     </td> 
 
     <td style="text-align: center;"> 
 
     <input type="number" name="variance[]" class="variance" value="" placeholder="Variance" readonly> 
 
     </td> 
 
    </tr> 
 
    </tbody> 
 
</table>

+1

謝謝這正是我一直在尋找將調查事件代表團再次感謝 – tyy007

+0

@ tyy007!。!請注意,不是構建html,Ive克隆了第一個元素,清除了輸入,並使用它來創建新行。恕我直言,它更容易處理,特別是對於複雜的html。 :) – DelightedD0D

+0

我有一個問題但是,如果tr被克隆,並且原始行沒有刪除行鏈接列,我應該如何添加刪除行鏈接作爲td? – tyy007

相關問題