gpt4 book ai didi

javascript - 删除 jQuery 添加的按钮

转载 作者:行者123 更新时间:2023-11-28 15:26:34 25 4
gpt4 key购买 nike

我正在使用 jQuery 将额外的选择和文本字段添加到表单中。不过,我希望能够使用删除按钮删除添加的文本字段。

一旦添加了一个字段,jQuery 似乎就无法检测到它。

jQuery

var counter = 2;

$("#addButton").click(function () {


var newTextBoxDiv = $(document.createElement('div'))
.attr("id", 'contact-list-div-' + counter).attr("class", 'contact-list-div');

newTextBoxDiv.after().html('<select></select>' +
'<input type="text" name="textbox' + counter +
'" id="textbox' + counter + '" value="" >' + '<button type="button" class="removeButton" id="removeButton-' + counter + '">Remove Button</button>');

newTextBoxDiv.appendTo("#contact-list");


counter++;
});


$(".removeButton").click(function() {
alert(this.id); //this never shows, only on the element that was
//added directly added using html, in this case removeButton-1
});

HTML

    <div id="contact-list">
<div class="contact-list-div" id="contact-list-div-1">
<select></select>
<input>
<button type='button' class='removeButton' id='removeButton-1'>Remove Button</button>
</div>
</div>
<input type='button' value='Add Button' id='addButton'>

最佳答案

$('#contact-list').on('click', '.removeButton', function() {
//Your code
});

关于javascript - 删除 jQuery 添加的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28766342/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com