如果选中了具有相同值的其他复选框,如何选中该复选框。如果选择 1,则应选中具有相同值的其他 div 复选框。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<div>
<input type="checkbox" value="1">1
<input type="checkbox" value="2">2
</div>
<div>
<input type="checkbox" value="1">1
<input type="checkbox" value="2">2
</div>
最佳答案
就这么简单:
$('input:checkbox').on('change', function(){
//if(this.checked) // optional, depends on what you want
$('input[value="' + this.value + '"]:checkbox').prop('checked', this.checked);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<div>
<input type="checkbox" value="1">1
<input type="checkbox" value="2">2
</div>
<div>
<input type="checkbox" value="1">1
<input type="checkbox" value="2">2
</div>
如果您希望在取消选中一个选项时仍保留其他选项,请查看 Arun P Johny's solution .
关于javascript - 如果选中其他具有相同值的复选框,则检查复选框 jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31475129/