gpt4 book ai didi

javascript - 更改结构时删除按钮不起作用

转载 作者:行者123 更新时间:2023-12-03 11:20:25 25 4
gpt4 key购买 nike

我动态添加按钮,并且使用此代码完美添加按钮,当我想删除用户通过单击“添加更多”按钮创建的文本框时效果非常好。

在点击事件上完美添加和删除按钮的代码。

这是 jquery 脚本!

$(document).ready(function() {

var MaxInputs = 8; //maximum input boxes allowed
var InputsWrapper = $("#InputsWrapper"); //Input boxes wrapper ID
var AddButton = $("#AddMoreFileBox"); //Add button ID

var x = InputsWrapper.length; //initlal text box count
var FieldCount=1; //to keep track of text box added

$(AddButton).click(function (e) //on add input button click
{
if(x <= MaxInputs) //max input box allowed
{
FieldCount++; //text box added increment
//add input box
$(InputsWrapper).append('<div class="controls"><input type="text" class="form-control" name="sub-category-name[]" required/><a href="#" class="removeclass">&times;</a></div>');
x++; //text box increment
}
return false;
});

$("body").on("click",".removeclass", function(e){ //user click on remove text
if( x > 1 ) {
$(this).parent('div').remove(); //remove text box
x--; //decrement textbox
}
return false;
})
});

这是 Html。

<div class="form-group" id="InputsWrapper">
<label for="sub-category-name">Sub Category Name </label>
<div class="controls">
<input type="text" class="form-control" id="typeahead" name="sub-category-name[]" required>
<a href="#" id="AddMoreFileBox" class="btn btn-info">Add More Field</a>
</div>
</div>

但是当我稍微更改代码结构时,用户可以添加文本框,但无法删除动态添加的文本框。
这是 Html 代码。

<div class="form-group" id="InputsWrapper">
<div class="input-group"><input type="text" class="form-control">
<span class="input-group-btn"> <a href="#" id="AddMoreFileBox"><button class="btn btn-default" type="button">+</button></a> </span>
</div>

</div>

这是脚本。

$(document).ready(function() {

var MaxInputs = 8; //maximum input boxes allowed
var InputsWrapper = $("#InputsWrapper"); //Input boxes wrapper ID
var AddButton = $("#AddMoreFileBox"); //Add button ID

var x = InputsWrapper.length; //initlal text box count
var FieldCount=1; //to keep track of text box added

$(AddButton).click(function (e) //on add input button click
{
if(x <= MaxInputs) //max input box allowed
{
FieldCount++; //text box added increment
//add input box
$(InputsWrapper).append('<div class="input-group"><input type="text" class="form-control"><span class="input-group-btn"> <a href="#" class="btn btn-default removeclass">X</a></span</div>');
x++; //text box increment
}
return false;
});

$("body").on("click",".removeclass", function(e){ //user click on remove text
if( x > 1 ) {
$(this).parent('div').remove(); //remove text box
x--; //decrement textbox
}
return false;
})
});

最佳答案

您需要通过标签和/或类名称来定位您的 parent :

$(this).closest("div").remove(); // walk up the DOM until the nearest DIV   

$(this).closest("div.form-group").remove(); // walk up to nearest div.form-group 

如果我得到了错误的容器,请将 form-group 更改为您希望删除的容器的类

关于javascript - 更改结构时删除按钮不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27142294/

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