javascript 特定类或 id 的所有复选框的 jQuery 选择器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16575299/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 05:11:16  来源:igfitidea点击:

jQuery selector for all checkboxes of a certain class or id

javascriptjqueryjquery-selectors

提问by DrewT

I have an advanced search form on a page and there is also a "clear" button which resets the form. As the form gets cleared I am using this code to un-check the form's checkbox inputs:

我在页面上有一个高级搜索表单,还有一个“清除”按钮可以重置表单。当表单被清除时,我使用此代码取消选中表单的复选框输入:

$('form#advanced_search :input[type=checkbox]').each(function() { this.checked = false; });

Problem is, this code resets all checkboxes on the page (tested in Chrome and FireFox) instead of just the ones in form#advanced_search.

问题是,此代码会重置页面上的所有复选框(在 Chrome 和 FireFox 中测试),而不仅仅是 form#advanced_search 中的复选框。

The same problem also happens using this selector method:

使用此选择器方法也会发生同样的问题:

$('form#advanced_search input:checkbox').each(function() { this.checked = false; });

I read somewhere that jQuery has some buggy issues with checkboxes and radios, but does anyone know a method or work around for this?

我在某处读到 jQuery 在复选框和收音机方面存在一些错误问题,但是有人知道一种方法或解决方法吗?

回答by sbeliv01

You're not using the correct selector to get the checkboxes that you want. Try this:

您没有使用正确的选择器来获取所需的复选框。试试这个:

$("#advanced_search input[type='checkbox']").prop("checked", false);

Fiddle: http://jsfiddle.net/R3Rx2/

小提琴:http: //jsfiddle.net/R3Rx2/

回答by Mimo

The selector is not correct: Try using:

选择器不正确:尝试使用:

$('form#advanced_search input[type=checkbox]').each(function() { this.checked = false; });

回答by DrewT

It looks like this is a datatables issue specific to my case. So I'll have to dig a bit deeper to solve the problem.The regular selector methods seem to be working with non-auto generated checkboxes on my page.

看起来这是一个特定于我的案例的数据表问题。所以我必须更深入地挖掘来解决这个问题。常规选择器方法似乎在我的页面上使用非自动生成的复选框。

I'll update here once I track down the bug.

一旦我追踪到错误,我会在这里更新。

Thanks everyone!

感谢大家!