开发者

How can I set an individual checkbox in a checkbox group to "checked" based on it's value attr?

开发者 https://www.devze.com 2023-01-04 03:29 出处:网络
<div id=\"notifydaydiv\" name=\"notifydaydiv\" style=\"display:none; padding-left:50px\"> <input name=\"notifyday\" type=\"checkbox\" value=\"wed\" /> Wednesday <br />
 <div id="notifydaydiv" name="notifydaydiv" style="display:none; padding-left:50px">
 <input name="notifyday" type="checkbox" value="wed" /> Wednesday <br />
 <input name="notifyday" type="checkbox" value="thu" /> Thursday <br />
 <span style="color:red; font-size:7pt;">Recommended</span><br />
 <input name="notifyday" type开发者_JAVA百科="checkbox" value="fri" checked="checked"/> Friday 
 <div id="day_err"></div>
 <script>
  [ set which checkbox is checked based on the value attr ]
 </script>
</div>


You can find and check it using the attribute-equals selector, like this:

$(":checkbox[value='wed']").attr('checked', true);

You can also use name in there if needed, like this:

$(":checkbox[name='notifyday'][value='wed']").attr('checked', true);


$(":checkbox[value='wed']").attr('checked', 'checked');

For more information, see the JQuery API.


$(document).ready(function(){
   $(":checkbox[value='wed']").attr('checked', 'checked');
});

Make sure that the page is loaded before you try and check the checkbox.

0

精彩评论

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