2011-02-14 69 views
0

我已經附加了輸入元素錶行到現有的表如下:我如何maniputate jQuery的追加到現有的表的錶行

$("#additems_table").append(
"<tr><td style='color:#999; font-size:16px;'>"+(counter++)+"</td><td> 
<select class='item'><?php while($product_rows=mysql_fetch_assoc($productSQL)){?><option value='<?php echo $product_rows['pid']; ?>'><?php echo $product_rows['name']; ?></option><?php }?></select></td><td id='quantity_td'><input name='quantity' class='quantity' type='text' style='width:35px' id='cost"+(numqty++)+"' /><input type='hidden' name='price_hf' id='cost"+(numprice++)+"'/></td><td id='amount_td'><input name='amount' id='cost"+(numamount++)+"' style='width:70px' type='text' value='3' disabled='disabled'/></td></tr>"); 
}); 

問題進來時,我試圖操縱中的這些元素以上附加的行。它根本不聽任何事件,點擊,改變等。例如

$("#additems_table input[type=text]").each(function(){ 
    $(this).change(function(){   
     alert("amount"); 
    }); 
}) 

我真的很感謝幫助。

+0

請你可以發佈一個格式化的代碼示例,理想的代碼傳遞給瀏覽器,而不是jQuery和php的組合。 – 2011-02-14 15:18:03

回答

1

OK,到線了新的輸入元素做線沿線的東西:

$(function(){ 

    $('#additems_table input[type="text"]').live('change', function(){ 

    }); 
}); 

這也就意味着:負載創建其內部的#additems_table所有元素的變化事件在全球處理器。實時方法可以實時創建新元素(輸入文本框),而無需擔心其事件註冊。新添加的元素的change事件將觸發並自動處理。