gpt4 book ai didi

javascript - 如何使用 jQuery .each 循环删除特定元素?

转载 作者:行者123 更新时间:2023-12-02 19:22:53 25 4
gpt4 key购买 nike

假设我有一个 HTML 文档,其中包含:

<form id = "my_form">
<input type = "text" />
<input type = "text" />
<input type = "text" />
<button type = "button" onclick = "removeGoodInputs()">Do the job</button>
</form>

我想删除满足某些条件的输入值(在我的 JS 中给出)。我尝试创建我的 removeGoodInputs() 函数(如下所示),但这会删除表单中的所有输入。我该如何解决这个问题?

function removeGoodInputs() {
$("#my_form input").each(function() {
if(this.attr("value") == 10)
$(this).remove();
});
}

最佳答案

解决此问题的另一种方法是使用 .filter [docs] :

$("#my_form input").filter(function() {
return this.value === '10';
}).remove();

关于javascript - 如何使用 jQuery .each 循环删除特定元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12340969/

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