JQuery select checkboxes with the same value
Is there a way in JQuery to check checkboxes based on their value.
e.g. if I have 3 checkboxes with the 开发者_开发问答value 'test' is there any way I can check them all based just on the fact that they have the value 'test'
$(":checkbox[value=test]").attr("checked", true);
See:
- http://api.jquery.com/checkbox-selector/
- http://api.jquery.com/attr/
- http://api.jquery.com/attribute-equals-selector/
Demo: http://jsfiddle.net/GNJ6t/
EDIT:
jQuery 1.6 introduces the .prop()
method, which is the supposed best way of achieving the above. From the manual:
The .prop() method should be used to set disabled and checked instead of the .attr() method.
So:
$(":checkbox[value=test]").prop("checked", true);
精彩评论