我检查所有复选框不起作用

时间:2015-08-21 11:00:40

标签: javascript php checkbox

我有这个表单从数据库中获取数据并显示复选框。我有一个功能来检查和取消选中所有框但它不工作。任何人都可以帮忙吗?

      <form id="myform" method="POST" action="PHPMailer/examples/mail-exec.php">
   <div class="row">
  My Trailers :</br>
  <div class="form-group" style="overflow-y: auto; height:100px;">
  <input type="checkbox" name="Check_ctr" value="yes"     onClick="Check(document.myform.check_list)"><b>Check Control</b> </br>
   <?php 
   $qry="SELECT * from followers where vendorid =$vendorid";
  $stmt = $conn->prepare( $qry );
  $stmt->execute();
  while($row = $stmt->fetch(PDO::FETCH_ASSOC))
 {?>
 <input type="checkbox" name="check_list" value="<?php echo      $row['email'];?>"><?php echo $row['name'];?><br>
 <?php } ?>     
 </div>                         
 </form>

这是js函数:

                <SCRIPT LANGUAGE="JavaScript">
                <!--

                <!-- Begin
                function Check(chk)
                {
                if(document.myform.Check_ctr.checked==true){
                for (i = 0; i < chk.length; i++)
                chk[i].checked = true ;
                }else{

                for (i = 0; i < chk.length; i++)
                chk[i].checked = false ;
                }
                }
                // End -->
                </script>

1 个答案:

答案 0 :(得分:-1)

您的控制台中出现Uncaught TypeError: Cannot read property 'check_list' of undefined错误

您需要指定表单的名称才能通过文档

访问它

function Check(chk, list) {
  var checked = document.myform.Check_ctr.checked
  for (i = 0; i < list.length; i++) {
    list[i].checked = checked;
  }
}
<form name="myform" method="POST" action="PHPMailer/examples/mail-exec.php">
  <div class="">My Trailers :
    <br/>
    <div class="form-group" style="overflow-y: auto; height:100px;">
      <input type="checkbox" name="Check_ctr" value="yes" onClick="Check(this, document.myform.check_list)" /> <b>Check Control</b> 
      <br/>
      <input type="checkbox" name="check_list" value="1">1
      <input type="checkbox" name="check_list" value="2">2
      <input type="checkbox" name="check_list" value="3">3
      <input type="checkbox" name="check_list" value="4">4
      <br/>
    </div>
</form>