可能重複:
Jquery adding event listeners to dynamically added elementsjQuery的:一個生成的按鈕不趕上click事件
jQuery的位:
我追加輸入字段的一些文本到DIV :
shareVisual = '<div class="individual-share">' + '<input type="number" value="1" /> X ' + classname.val() + '@' + classcurrency.val() + ' ' + classprice.val() + ' per share ' + '<button type="button" class="remove-share">Remove</button></div>';
listOfSharesBox.append(shareVisual);
然後我嘗試去捕捉點擊事件:
$("#list-of-shares").bind('click', '.remove-share', function() {
$(".remove-share").closest("div").find(".individual-share").remove();
});
爲什麼DIV不會被刪除?
乾杯。
P.S.
當我改變我的代碼如下:
$("#list-of-shares").bind('click', '.remove-share', function() {
$(".remove-share").closest("div").remove();
});
的所有動態生成的投入得到專區內刪除。
使用'on'方法http://api.jquery.com/on/ – undefined