[英]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.