javascript - 使用 javascript 或 JQuery 在 HTML 中删除当前分区的按钮

标签 javascript jquery html removeclass

我制作了一个带有添加按钮的动态表单,该按钮使用表单的克隆创建了一个新的部门,但具有不同的 ID。但是,我还希望每个分区都有一个删除按钮,删除它所在的分区,包括按钮本身。

我熟悉 JQuery .remove() 方法,但我无法选择按钮所在的分区。

<div id="attendees">
        <div id="att1" class="attendee">
            <form name="droplist" action="html_form_action.asp" method = "get">
                <fieldset>
                    <legend><span class="legend">Filter</span></legend>
                    <label for="select">Category: </label>
                    <select id="select" size="1"  >
                        <option value=" " selected="selected"> </option>
                        <option value="employees">Number of Employees</option>
                        <option value="hits">Hits on TechCrunch</option>
                        <option value="time_founded">Time Founded</option>
                        <option value="total_money">Total Money</option>
                    </select>

                    <div id='radioContainer' style="display:block;">
                        <input type="radio" id="condition"  name="button" value="more"/> <label for="condition"> > </label></br>
                        <input type="radio" id="condition1"  name="button" value="less"/> <label for="condition1"> < </label>
                    </div>

                    <div id='radioContainerTime' style="display:block;">
                        <input type="radio" id="condition2"  name="button" value="later"/> <label for="condition"> After </label></br>
                        <input type="radio" id="condition3"  name="button" value="earlier"/> <label for="condition1"> Before </label>
                    </div>

                    <div id = "text_box" style="display:block;">
                        <label for="input_value">Amount: </label>
                        <input type="text" id="box" name="input_value" value="textIn" />
                    </div>
                    <button>remove</button>
                </fieldset>
            </form>
        </div>
        <a href="#" class="add">Add more</a>
</div>​

到目前为止,我的 JavaScript 是这样的:

$(function(){
 var template = $('#attendees .attendee:first').clone(),
     attendeesCount = 1;

 var addAttendee = function(){
     attendeesCount++;
     var attendee = template.clone().find(':input').each(function(){
         var newId = this.id.substring(0, this.id.length-1) + attendeesCount;
         $(this).prev().attr('for', newId); // update label for (assume prev sib is label)
         this.name = this.id = newId; // update id and name (assume the same)
     }).end() // back to .attendee
     .attr('id', 'att' + attendeesCount) // update attendee id
     .prependTo('#attendees'); // add to container
 };

 $('.add').click(addAttendee); // attach event
 }); 

$("button").click(function () {
  $this.remove();
});

最佳答案

删除<div class="attendee">包含按钮,你可以使用这个:

$("button").click(function () {
     $(this).closest(".attendee").remove();
     return(false);
});

这会从单击的按钮查找父链,以找到带有 class="attendee" 的第一部分然后将其删除。


仅供引用,我强烈建议在您的删除按钮上放置一个类,因为连接到您页面中的所有按钮有点危险。

要在删除按钮上放置一个类,请将 HTML 更改为:

<button class="deleteButton">remove</button>

然后,将 jQuery 更改为:

$(".deleteButton").click(function () {
     $(this).closest(".attendee").remove();
     return(false);
});

return(false)防止基于点击发生表单中的任何默认操作。

关于javascript - 使用 javascript 或 JQuery 在 HTML 中删除当前分区的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12080289/

相关文章:

javascript - 使用 jQuery 更改图像源

javascript - mustache 空值

javascript - 是否可以在实例化的 StreetViewPanorama 中显示叠加层/标记?

javascript - 使用onClick从javascript创建html按钮调用一个参数的方法

html - 如何在 IE 中水平/垂直居中?

css - 页面上的多个 <progress> 元素具有不同的颜色

javascript - 我们可以使用 jquery 填充 Gmail 文本字段吗?

javascript - 如何使图像随页面调整大小

javascript - 需要有关 ajax 请求的帮助

横幅中的 JavaScript 文本按时间间隔更改 - 无点击