douyi3676 2016-02-17 09:10
浏览 60
已采纳

复选框选择所有php javascript

I made a script in order to create a select all option for items in a form.

<script>
        function Check(frm){
        var checkBoxes = frm.elements['patients[]'];

        for (i = 0; i < checkBoxes.length; i++){
          checkBoxes[i].checked = (checkBoxes[i].checked != true ) ? true : false;
        }

        }

        window.onload = function(){
            document.getElementById("selectpatient").onchange = function(){Check(document.selectform)};
        };
</script> 

This works fine with the following sample code.

<body>
<form name="selectform" method="" action="">
<label for="red">Red</label>
<input type="checkbox" name="patients[]" value="red" id="red"/><br />

<label for="blue">Blue</label>
<input type="checkbox" name="patients[]" value="blue" id="blue"/><br />

<label for="green">Green</label>
<input type="checkbox" name="patients[]" value="green" id="green"/><br />

<label for="black">Black</label>
<input type="checkbox" name="patients[]" value="black" id="black"/><br /><br />

<label for="selectall" id="selectControl">Select All</label>
<input type="checkbox" id="selectall" />
</form>

<script>
function Check(frm){
var checkBoxes = frm.elements['patients[]'];

for (i = 0; i < checkBoxes.length; i++){
checkBoxes[i].checked = (checkBoxes[i].checked != true ) ? true : false;
}

}

window.onload = function(){
document.getElementById("selectall").onchange = function() Check(document.selectform)};
};
</script>
</body>

However, I have a separate php code in which I am trying to run a code to be able to select items from a database.The Php segment is as follows.

            <div class="row"><form name="selectform" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>" method="post">
        <div class="box clearfix">
        <table class="table" >
            <thead>
            <tr>
                <th><input type="checkbox" id='selectall' ></th>

                <th>First Name</th>
                <th>Last Name</th>
                <th>NIC</th>
                <th>Address</th>

                <th>Telephone/Mobile</th>
                <th>Disability</th>
                <th>Reason</th>
                <th>Description</th>
                <th></th>
            </tr>
            </thead>
            <tbody>
            <?php 
                while ($data = $res->fetch_assoc()){
                    echo "<tr><td><input type='checkbox' name='patients[]' id='patients[]' value='".$data['PatientID']."'></td><td>".$data['First_Name']."</td><td>".$data['Last_Name']."</td><td>".$data['NIC_No']."</td><td>".$data['Address']."</td><td>".$data['Telephone']."</td><td>".$data['Disability']."</td><td>".html_entity_decode($data['Reason'])."</td><td>".html_entity_decode($data['Description'])."</td>";
                }
            ?>
            </tbody>
        </table>
    </div>

                </form>
                <script>
                        function Check(frm){
                        var checkBoxes = frm.elements['patients[]'];

                        for (i = 0; i < checkBoxes.length; i++){
                          checkBoxes[i].checked = (checkBoxes[i].checked != true ) ? true : false;
                        }

                        }

                        window.onload = function(){
                          document.getElementById("selectall").onchange = function(){Check(document.selectform)};
                        };
                   </script>

    </div>

The above code does not execute the select all script. Please suggest as to what I can do to make this functional.

  • 写回答

1条回答 默认 最新

  • dongwuge6201 2016-02-17 09:23
    关注

    Seems that you are missing { of the onchange callback:

    function Check(frm) {
      var checkBoxes = frm.elements['patients[]'];
    
      for (i = 0; i < checkBoxes.length; i++) {
        checkBoxes[i].checked = (checkBoxes[i].checked != true) ? true : false;
      }
    
    }
    
    window.onload = function() {
      document.getElementById("selectall").onchange = function() { // <--missing { here
        Check(document.selectform)
      };
    };
    <form name="selectform" method="" action="">
      <label for="red">Red</label>
      <input type="checkbox" name="patients[]" value="red" id="red" />
      <br />
    
      <label for="blue">Blue</label>
      <input type="checkbox" name="patients[]" value="blue" id="blue" />
      <br />
    
      <label for="green">Green</label>
      <input type="checkbox" name="patients[]" value="green" id="green" />
      <br />
    
      <label for="black">Black</label>
      <input type="checkbox" name="patients[]" value="black" id="black" />
      <br />
      <br />
    
      <label for="selectall" id="selectControl">Select All</label>
      <input type="checkbox" id="selectall" />

    Or you can simplify this more as:

    function Check(frm, ischecked) {
      var checkBoxes = frm.elements['patients[]'];
    
      for (i = 0; i < checkBoxes.length; i++) {
        checkBoxes[i].checked = ischecked; // and just update for all here
      }
    
    }
    
    window.onload = function() {
      document.getElementById("selectall").onchange = function() { // <--missing { here
        Check(document.selectform, this.checked); // <----pass the state here
      };
    };
    <form name="selectform" method="" action="">
      <label for="red">Red</label>
      <input type="checkbox" name="patients[]" value="red" id="red" />
      <br />
    
      <label for="blue">Blue</label>
      <input type="checkbox" name="patients[]" value="blue" id="blue" />
      <br />
    
      <label for="green">Green</label>
      <input type="checkbox" name="patients[]" value="green" id="green" />
      <br />
    
      <label for="black">Black</label>
      <input type="checkbox" name="patients[]" value="black" id="black" />
      <br />
      <br />
    
      <label for="selectall" id="selectControl">Select All</label>
      <input type="checkbox" id="selectall" />

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路