我的 HTML 中有这个项目:
<p class="choose-item" data-product-id = "{{ listprod.id }}"></p>
我需要使用数据属性删除它。我发现什么不起作用:
$(".choose-item").find("[data-product-id=" + Model.get('old_prod_id') + "]").remove();
其中需要 Model.get('old_prod_id') 项目 ID。有人可以帮我做这件事吗?
最佳答案
你应该使用.filter()
方法而不是.find()
,.find()
选择被选元素的后代元素, 但在这里你想要 .filter()
集合(选择的元素)。
$(".choose-item").filter("[data-product-id='" + Model.get('old_prod_id') + "']")
.remove();
关于javascript - 按数据属性查找和删除项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19065868/