如果选中一个复选框,则会检查另一个复选框

时间:2015-04-10 11:02:48

标签: javascript

我有很多不同价值的复选框。

for ($i=0;$i<10;$i++) {
  echo "<input type='checkbox' name='id[]'> <input type='checkbox' name='title[]'>
}

如果我选中复选框名称ID并且复选框名称标题也会检查怎么办?

3 个答案:

答案 0 :(得分:0)

通过JS试试..

 <?php
    echo '<input type="checkbox" id="chk1" name="id[]"> <input type="checkbox" name="title[]" id="chk2">';
    ?>
<script>
      $('#chk1').click(function(){
         if($("#chk1:checked").length)
             $('#chk2').prop('checked', true);
      });
</script>

答案 1 :(得分:0)

$("input[type='checkbox'][name='id[]']").on('change',function(){
var isChecked = $("input[type='checkbox'][name='id[]']").prop('checked');
if(isChecked ){
$("input[type='checkbox'][name='title[]']").prop('checked', true);
}
else{
$("input[type='checkbox'][name='title[]']").prop('checked', false);
}
});  

<强> demo

答案 2 :(得分:0)

使用此JS代码

<input type="checkbox" id="check">

<input type="checkbox" class="chk">
<input type="checkbox" class="chk">
<input type="checkbox" class="chk">
<input type="checkbox" class="chk">
<script>

var x=document.getElementById('check').checked;
if(x==true)
{
 var length=document.getElementsByClassName('chk').length; 
 for(i=0;i<=length; i++)
{
 document.getElementsByClassName('chk')[i].checked=true;
}    
}
else
{
 var length=document.getElementsByClassName('chk').length; 
 for(i=0;i<=length; i++)
{
 document.getElementsByClassName('chk')[i].checked=true;
}

}

</script>
相关问题