简体   繁体   中英

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

 <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="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');

有关更多信息,请参见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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM