开发者

How do I select all checkbox with a unique name?

开发者 https://www.devze.com 2023-02-07 07:31 出处:网络
I am creating checkboxes with a loop.I would like to uncheck all checkboxes that START with the name \"NameCheckBox\" with id \"nameChk\".Therefore all the following will not be set to check.

I am creating checkboxes with a loop. I would like to uncheck all checkboxes that START with the name "NameCheckBox" with id "nameChk". Therefore all the following will not be set to check.

<input id='nameChk1' name='NameCheckBox1' type='checkbox' />
<input id='nameChk2' name='NameCheckBox2' type='checkbox' />
<input id='name开发者_如何学PythonChk4' name='NameCheckBox4' type='checkbox' />

and the following will not be affected

<input id='dateChk1' name='DateCheckBox1' type='checkbox' />
<input id='dateChk2' name='DateCheckBox2' type='checkbox' />
<input id='dateChk3' name='DateCheckBox3' type='checkbox' />

How do I do this?


you can use the starts-with attribute selector to target the elements you want..

$('input:checkbox[name^=NameCheckBox][id^=nameChk]').attr('checked', false);


$('input:checkbox[name^=NameCheckBox]').removeAttr('checked');

You can do the same for id instead of name. However, using a common class name is better and most likely faster.


You'll want to use attr() to get the name and/or id of the items...

http://jsfiddle.net/GzTpp/2/


That's not a good way of doing it. You're better off identifying these name fields with some kind of css class.

<input id="foo" class="namecheck">

then use $(".namecheck").attr("checked", false); to uncheck them when you need to.

0

精彩评论

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