简体   繁体   English

删除带有 Jquery 的列表项元素

[英]Delete a list item element with Jquery

I made a simple form that takes an input name and when I click submit, it will append the output name into an unordered list with a delete button beside each item.我制作了一个简单的表单,它接受一个输入名称,当我单击提交时,它将 append output 名称放入一个无序列表中,每个项目旁边都有一个删除按钮。 But when I click the delete (X) button, it still leave the X button there, and when I have multiple list, it remove all the list instead of the list that corresponds with each item.但是,当我单击删除 (X) 按钮时,它仍然会将 X 按钮留在那里,并且当我有多个列表时,它会删除所有列表而不是与每个项目对应的列表。

看法 视图1

 $("button#submit").click((e) => { e.preventDefault(); let name = $("#name").val(); console.log(name); $("ul#submittedName").append( "<div class='container'><div class='row'><div class='col-sm'><li id='nameList'>" + name + "</div><div class='col-sm'><a class='delete' href='#'>X</a></div></div></div></li>" ); $("#name").val(""); }); $("ul").on("click", ".delete", function(e) { console.log(e.target); $("li#nameList").remove(); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous" /> <form id="formName" class="align-center p-3 mx-auto"> <div class="mb-3"> <label for="exampleInputEmail1" class="form-label">Name</label> <input type="text" class="form-control" id="name" aria-describedby="emailHelp" /> </div> <button id="submit" type="submit" value="Submit" class="btn btn-primary"> Submit </button> </form> <ul id="submittedName" class="align-center p-3 mx-auto"></ul>

As stated in the comments, id's are meant to be unique, but you can do this without the use of the id at all, and since you want to remove the delete button as well, just get the ancestor div.container and remove it, this will remove the li and the delete button:如评论中所述,id 应该是唯一的,但您可以在不使用 id 的情况下执行此操作,并且由于您也想删除删除按钮,只需获取祖先div.container并将其删除,这将删除li和删除按钮:

$("ul").on("click", ".delete", function(e) {
   $(this).closest('div.container').remove();
});

If you fix your HTML to be complient, all you need to do is, $(this).closest('li').remove() in your event handler.如果您将 HTML 修复为兼容,则您需要做的就是在事件处理程序中$(this).closest('li').remove() Here is an HTML complient example for what you are trying to do这是您要执行的操作的 HTML 兼容示例

 $(function(){ $("button#submit").click((e) => { e.preventDefault(); let name = $("#name").val(); $("ul#submittedName").append("<li>" + name + "&nbsp;<a class='delete' href='#'>X</a></li>"); $("#name").val(""); }); $("ul").on("click", ".delete", function(e) { $(this).closest('li').remove(); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous" /> <form id="formName" class="align-center p-3 mx-auto"> <div class="mb-3"> <label for="exampleInputEmail1" class="form-label">Name</label> <input type="text" class="form-control" id="name" aria-describedby="emailHelp" /> </div> <button id="submit" type="submit" value="Submit" class="btn btn-primary"> Submit </button> </form> <ul id="submittedName" class="align-center p-3 mx-auto"></ul>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM