我制作了一个脚本,以便为表单中的项目创建一个全选选项。
<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>
上面的代码不执行select all脚本。请建议我能做些什么来使其发挥作用。
答案 0 :(得分:0)
似乎您遗失了{
回调的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" />