开发者

JQuery select checkboxes with the same value

开发者 https://www.devze.com 2023-01-15 16:48 出处:网络
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

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);
0

精彩评论

暂无评论...
验证码 换一张
取 消