0

可能重复:
Jquery 将事件侦听器添加到动态添加的元素

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 没有被删除?

干杯。

附言

当我将代码更改为此:

$("#list-of-shares").bind('click', '.remove-share', function() {
    $(".remove-share").closest("div").remove();
});

所有动态生成的输入都会在 div 中删除。

4

2 回答 2

2

如果您使用 jquery 1.7 意味着尝试这样

$(".remove-share").on('click',function() {
    $(this).closest("div").remove();
});

否则使用

$(".remove-share").bind('click',function() {
    $(this).closest("div").remove();
});
于 2013-01-23T10:22:59.403 回答
-1

尝试使用处理动态生成控件的“live”关键字:

$(".remove-share").live('click', function() {
$(this).closest("div").remove();
});
于 2013-01-23T10:30:54.983 回答