Checking the checkbox even I set it into disabled

我的梦境 提交于 2019-12-24 14:29:47

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!