jQuery - 如何选择除特定复选框之外的所有复选框?
我有一个看起来像这样的 jQuery 选择器...
$("input:checkbox").click(function(event) {
// DO STUFF HERE
}
一切都运行良好,直到我被要求添加另一个与原始复选框无关的复选框。如何为除一个复选框之外的所有复选框创建选择器?谢谢。
I have a jQuery selector that looks like this ...
$("input:checkbox").click(function(event) {
// DO STUFF HERE
}
Everything was working well until I was asked to add another checkbox that has nothing to do with the original checkboxes. How do I create a selector for all checkboxes except for one? Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
只需为相关复选框指定一个唯一的
id=""
Just give the checkbox in question a unique
id=""
您可以执行以下操作:
将“0”作为所需复选框的索引(在本例中为 DOM 上的第一个)
You can do the following:
being the "0" the index of your desired checkbox (in this case the first on the DOM)
嗯..我们需要更多地了解 HTML 才能得到具体的答案,但我能想到两种方法。
1) 如果您知道不需要的名称,则使用
not
删除该名称2) 使用正确输入的共同点来选择它们。
Well.. we need to know more about the HTML for a specific answer, but there are two methods that I can think of.
1) if you know the name of the one that you don't want then remove that one with
not
2) use something that the correct inputs have in common to select them.
如果所有复选框都在另一个元素内,您可以选择该元素内的所有复选框。
下面是一个具有一个父元素的示例。
下面的 jQuery 也适用于多个 div:
jQuery:
仅选择任何具有“test”类的元素中的复选框。
-Sunjay03
If all the checkboxes are within another element, you can select all checkboxes within that element.
Here is an example with one parent element.
The jQuery below also works with multiple divs as well:
jQuery:
That selects just the checkboxes within any element with class "test".
-Sunjay03
我使用了这个 - 不想选择以“订阅”命名的复选框,所以我使用了“$(':checkbox:not(checkbox[name=nameofyourcheckboxyoudontwantto]').each(function(){”
在此处输入图像描述
i used this - dont want to select the checkbox named with "subscription" so i used "$(':checkbox:not(checkbox[name=nameofyourcheckboxyoudontwantto]').each(function(){"
enter image description here