PHP复选框获胜't撤消禁用属性,但将取消选中

PHP Checkboxes won't undo disable attribute, but will uncheck

本文关键字:属性 取消 获胜 复选框 撤消 PHP      更新时间:2023-09-26

我有几页长代码,但我很难让它发挥作用。下面的脚本在一个主复选框上使用onclick命令来检查和禁用具有特定ID的所有复选框。当用户取消单击该框时,复选标记将消失,但仍处于禁用状态。即使他们按下了重置按钮,禁用的盒子也会保留下来。

Javascript:

 <script>
 function checkAllbase(bx) {
  var cbs = document.getElementsByTagName('input');
  for(var i=0; i < cbs.length; i++) {
    if(cbs[i].id == '1') {
      cbs[i].checked = bx.checked;
      cbs[i].disabled=true;
    }
  }
}
 </script> 

为每个与包关联的复选框输入提供类似的名称或类。在看不到实际html的情况下,在下方提供示例html

HTML

<input type="checkbox" class="package1" name="package1[]" value="vacation">
<input type="checkbox" class="package1" name="package1[]" value="bonus">
<input type="checkbox" class="package1" name="package1[]" value="fries">

JS-

function togglePackage(isChecked,packageName) {
  //Choose one of the two below to use
  //Below selects the inputs by the class name
  var cbs = document.querySelectorAll("input."+packageName);
  //Or Below selects the inputs by the input name
  var cbs = document.querySelectorAll("input[name^="+packageName+"]");
  for(var i=0; i<cbs.length; i++) {
        //Since they are either checked and disabled (both true)
        //or unchecked and enabled (both false) we can just use
        //isChecked to set both at once.
        cbs.checked = isChecked;
        cbs.disabled = isChecked;
  }
}
//Then somewhere call the togglePackage function
//bx here would be the main checkbox that you want to 
//have toggling the other boxes, and "package1" is obviously
//the name or the class you gave the checkboxes.
togglePackage(bx.checked,"package1");

元素保持为disabled,因为要设置的值是常量(文字)true

你可以做的是复制bx.disabled,就像你在使用bx.checked:一样

cbs[i].checked = bx.checked;
cbs[i].disabled = bx.disabled;

或者添加另一个参数以传递所需的disabled状态:

function checkAllbase(bx, disabled) {
  disabled = disabled !== false; // limit to `true` or `false`, prefer `true`
  // ...
    cbs[i].checked = bx.checked;
    cbs[i].disabled = disabled;
  // ...
}
// usage
checkAllbase(bx);        // disables elements
checkAllbase(bx, true);  // also disables elements
checkAllbase(bx, false); // enabled elements