开发者

jquery sselect checkbox1 when checkbox2 or checkbox3 are seleted

How can I say if you click on checkbox2 or checkbox3 and select one of them, then force checkbox1 to be selected as well

$('input:checkbox').click(function() {
 if ($(this).attr('class') == 'checkbox2' || $(this).attr('class') == 'checkbox3')
  {   
    if($(this).attr('checked',true))
    {
      $(this).parent().....
    }
}

});

<TD> 

<div class='checkboxes'>
<LABEL style="white-space: nowrap;">
<INPUT type="checkbox" name="user_registered[8253]" value="36" class="checkbox1" >
</LABEL><BR>     

<LABEL style="white-space: nowrap;">
<INPUT type="checkbox" name="user_registered[8253]" value="14" class="checkbox2" >
</LABEL><BR>     

<LABEL style="white-space: nowra开发者_Go百科p;">
<INPUT type="checkbox" name="user_registered[8253]" value="14" class="checkbox3" >
</LABEL><BR>     

</div>
</TD>

<TD> 

<div class='checkboxes'>
<LABEL style="white-space: nowrap;">
<INPUT type="checkbox" name="user_registered[8251]" value="36" class="checkbox1" >
</LABEL><BR>     

<LABEL style="white-space: nowrap;">
<INPUT type="checkbox" name="user_registered[8251]" value="14" class="checkbox2" >
</LABEL><BR>     

<LABEL style="white-space: nowrap;">
<INPUT type="checkbox" name="user_registered[8251]" value="14" class="checkbox3" >
</LABEL><BR>     

</div>
</TD>


Basically, you can put a click function onto checkbox2 and checkbox3 so that, when they are clicked, they check to see if they are now selected. If so, then checkbox1 should be selected.

Of course, you can add additional work to unselect checkbox1 if both are unselected, but that wasn't specified in your question. (Just add an else if to the statement.)

$('input.checkbox2, input.checkbox3').click(function() {
    if ($(this).is(':checked')) {
        $(this).siblings('input.checkbox1').attr('checked', true);  
    }
});

If the checkbox2 and checkbox3 aren't direct siblings, you may need to utilize the name so that you can properly select the correct checkbox1 which is easy enough to do. My brain is fried, though, and I need sleep. Hope this helps!

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜