最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery selector for all checkboxes of a certain class or id - Stack Overflow

programmeradmin0浏览0评论

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.

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?

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.

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?

Share Improve this question edited May 15, 2013 at 21:41 BoltClock 725k165 gold badges1.4k silver badges1.4k bronze badges asked May 15, 2013 at 21:31 DrewTDrewT 5,0822 gold badges43 silver badges54 bronze badges 10
  • Do you have multiple form's ? – Adil Shaikh Commented May 15, 2013 at 21:34
  • The only way I can explain this is if you have more than one form with the same ID. Otherwise, it doesn't make any sense to me. Your :input[type=checkbox] selector is wrong (remove the colon), but that shouldn't cause this. – Justin Morgan Commented May 15, 2013 at 21:36
  • Check $('form#advanced_search input:checkbox').length -- if it's 0 then your selector is wrong. – Cᴏʀʏ Commented May 15, 2013 at 21:37
  • @JustinMorgan It is not wrong. It selects all input elements that also have attribute type equals to checkbox. – VisioN Commented May 15, 2013 at 21:37
  • @VisioN: Neither of the two selectors here is used by querySelectorAll(). – BoltClock Commented May 15, 2013 at 21:40
 |  Show 5 more ments

3 Answers 3

Reset to default 3

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/R3Rx2/

The selector is not correct: Try using:

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

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!

发布评论

评论列表(0)

  1. 暂无评论