6

我在多个 div 中有以下复选框。

<div class="div1">
    <input type="checkbox" class="chkbox" value="101"> This is 101
    <input type="checkbox" class="chkbox" value="102"> This is 102
    <input type="checkbox" class="chkbox" value="103"> This is 103
</div>
<div class="div2">
    <input type="checkbox" class="chkbox" value="110"> This is 110
    <input type="checkbox" class="chkbox" value="102"> This is 102
    <input type="checkbox" class="chkbox" value="101"> This is 101
</div>

如上所示,一些复选框在多个 div 中具有相同的值(例如,101)。现在,每当检查复选框时,我都需要检查具有相同值的其他复选框。同样,取消选中。

$(".chkbox").change(function() {
    // If checked
    if (this.checked)
           // find other checkboxes with same value and check them
    else
           // find other checkboxes with same value and uncheck them
}
4

5 回答 5

9

你可以这样做:

$(".chkbox").change(function() {
    var val = $(this).val();
  if( $(this).is(":checked") ) {

    $(":checkbox[value='"+val+"']").attr("checked", true);
  }
    else {
        $(":checkbox[value='"+val+"']").attr("checked", false);
    }
});

演示:jsFiddle

于 2013-01-04T06:36:52.623 回答
3
$(".chkbox").change(function() {
    $(":checkbox[value="+$(this).val()+"]").prop("checked", this.checked);
}
于 2013-01-04T06:36:44.567 回答
0

你可以这样做。

现场演示

$(".chkbox").change(function() {
    // If checked
    if (this.checked)
           $(":checkbox[value=" + this.value + "]").attr('checked', true);
    else
           $(":checkbox[value=" + this.value + "]").attr('checked', false);           // find other checkboxes with same value and uncheck them
})

您错过了)更改事件的结束括号。

于 2013-01-04T06:36:45.113 回答
0
$( ".chkbox" ).change(function() {
  var value = $( this ).attr( "value" );

  $( ".chkbox[value='" + value + "']" ).prop( "checked", this.checked );
});
于 2013-01-04T06:39:24.433 回答
0
// if user has already sent the survey
    if(user_id_value){

        var result = confirm("This user already received a survey. Are you sure you want to resend?");

        // do not send the survey again
        if(!result){            
            $(":checkbox[value='"+user_id+"']").attr("checked", false);         
        }// end if not confirmed

    }// end if 
于 2014-06-30T11:10:06.483 回答