2014-01-23 22 views
0

我已經動態生成了jQuery的表數據。最初,當我用php做的keyup事件工作完美時,我能夠在T.Amount列中找到.qestimate和#number_of_houses產品的每一行,但現在它不工作,代碼列表Keyup事件不能與jQuery生成的字段

HTML:

      <table class="tg tg-table-light-1" border=""> 
           <thead> 
            <tr> 
             <th>No.</th> 
             <th>Material</th> 
             <th>Qty</th>         
             <th>T. Amount</th>  
             </tr> 
           </thead> 
          </table> 

JQuery的:

$(document).ready(function() { 

    table=$('.tg-table-light-1'); 




    $.getJSON("<?php echo base_url() . 'material_estimation/loadEstimates' ?>/", function(materialdetails) { 
     i=1; 
    $.each(materialdetails , function(i, materials){ 
    newmaterials= materials.Materials; 
    sanitized_newmaterials=newmaterials.replace(/<\/?([a-z][a-z0-9]*)\b[^>]*>?/gi, ''); 
    data="<tr class=\"tg-even\"> 

    <td class=\"\">"+i+"</td>\n\ 
        <td class=\"tg-even\">"+sanitized_newmaterials+"</td>\n\ 
        <td class=\"\"><input type=\"text\" style=\"width:40px; text-align: center;\" class=\"qestimate\" name=\"qestimate[]\" value=\""+materials.Quantity_estimate+"\"/></td>\n\ 
       <td class=\"\"><input type=\"text\" style=\"width:40px; text-align: center;\" class=\"tqty\" name=\"tqty[]\" value=\""+materials.total_quantity+"\"/></td>\n\ 

            </tr>"; 
        table.append(data); 

        i++; 
        });       
       }); 

//觸發事件

$(".tg-even input").keyup(multInputs); 

//我打電話,但沒有工作

function multInputs() { 
       var mult = 0; 

       $("tr.tg-even").each(function() { 
        // get the values from this row: 
        var $val1 = $('#number_of_houses option:selected').val(); 
        var $val2 = $('.qestimate', this).val(); 
        var $total = ($val1 * 1) * ($val2 * 1); 
        var qty = $('.tqty', this).val($total); 


       }); 


     } 

})

回答

6

和使用授權的功能:

$(document).on('keyup', '.tg-even input', function(e){ 
    // ... 
}); 

或者,如果你想:

$(document).on('keyup', '.tg-even input', multInputs); 
1

電話:

$(".tg-even input").keyup(multInputs); 

創建動態字段後..

0

在實際項目中,我們不應該添加代表團觸發直接記錄,或者,如果您有任何其他地方輸入一些文字,性能是一個問題。就像這樣:

$('.tg-even').on('keyup', 'input', function(e){ 
    // ... 
});