繁体   English   中英

复选框选择所有php javascript

[英]Checkbox Select all php javascript

我创建了一个脚本,以便为表单中的项目创建全选选项。

<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> 

使用下面的示例代码可以很好地工作。

<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>

但是,我有一个单独的php代码,我试图在其中运行代码以能够从数据库中选择项目.php部分如下。

            <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>

上面的代码不执行全选脚本。 请建议我该怎么做才能发挥作用。

似乎您缺少{ onchange回调中的{

 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" /> 

或者,您可以将其简化为:

 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" /> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM