问题
Why even I set the checkbox into disable it always check all when I click the script of check all.
Here's my script
Disable checkbox set by php code
while ($row = $result1->fetch_assoc()) {
<td><input name="checkbox[]" type="checkbox" id="checkbox[]" value="' . $row['id'] . '"'.($row['pr'] == ""?"disabled ":"").' class="checkbox"></td>
}
And here's my script on check all
function setCheckboxes3(act) {
var e = document.getElementsByClassName('checkbox');
var elts_cnt = (typeof(e.length) != 'undefined') ? e.length : 0;
if (!elts_cnt) {
return;
}
for (var i = 0; i < elts_cnt; i++) {
e[i].checked = (act == 1 || act == 0) ? act : (e[i].checked ? 0 : 1);
}
}
Links
a href="javascript:setCheckboxes3(1);" class="chkmenu">Check All</a> | <a href="javascript:setCheckboxes3(0);" class="chkmenu">Uncheck All</a>
回答1:
Update the links to pass true|false
<a href="javascript:setCheckboxes3(true);" class="chkmenu">Check All</a> | <a href="javascript:setCheckboxes3(false);" class="chkmenu">Uncheck All</a>
then
function setCheckboxes3(act) {
$('.checkbox').not(':disabled').prop('checked', act)
//or $('.checkbox:not(:disabled)').prop('checked', act)
}
with your current links
function setCheckboxes3(act) {
$('.checkbox').not(':disabled').prop('checked', act == 1 ? true : false);
//or $('.checkbox:not(:disabled)').prop('checked', act == 1 ? true : false)
}
回答2:
I know it's messy but it should get the job done:
for (var i = 0; i < elts_cnt; i++) {
if(!e[i].disabled){
e[i].checked = (act == 1 || act == 0) ? act : (e[i].checked ? 0 : 1);
}else{
e[i].checked = false;
}
}
回答3:
You could just check it if it's not disabled, something like:
e[i].checked = (act == 1 || act == 0)? act : e[i].checked && !e[i].disabled ? false : true;
The checked property is boolean, so it should be set it to true
or false
来源:https://stackoverflow.com/questions/20113113/checking-the-checkbox-even-i-set-it-into-disabled