:checked is selecting select inputs as well
I have a form that has select inputs as well as checkboxes. The following selector:
$(':checked')
selected both, so I had to use:
$(':checkbox:c开发者_如何学Checked')
to only select the checkboxes that were checked.
Q: Did I find a bug in jQuery, or is it just my understanding?
technically select elements, radio buttons, and checkboxes have a checked state, so it's not a bug.
input-checked
from jQuerys documentation :checked
should not work for select
elements so you might be right... testing in jsfiddle
The :checked selector works for checkboxes and radio buttons. For select elements, use the :selected selector.
From this test: http://jsfiddle.net/hunter/zAC54/
I'm not seeing the results that you're seeing...
Have you added a checked
property to the select
inputs through code?
:checked
is not what you want if you just want to check for checkboxes
$("input:checked") //selector for checked radio buttons and checkboxes
$("input:radio:checked") //selector for checked radio buttons
$("input:checkbox:checked") //selector for checked checkboxes
Finally it is not a good idea for performance reasons to use class selectors and other special selectors by themselves with out a tagname. The reason for this is jQuery will run though all the dom elements looking for that class or selector.. If you apply a tag name jquery will attempt to use getElementsByTagName first then iterate through only those elements with the following tag name. It is quicker and more efficient.
精彩评论