Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Checkbox PHP复选框赢得';t撤消禁用属性,但将取消选中_Checkbox_Javascript - Fatal编程技术网

Checkbox PHP复选框赢得';t撤消禁用属性,但将取消选中

Checkbox PHP复选框赢得';t撤消禁用属性,但将取消选中,checkbox,javascript,Checkbox,Javascript,我有几页很长的代码,但我很难让它正常工作。下面的脚本使用onclick命令在一个主复选框上选中并禁用具有特定ID的所有复选框。当用户松开该框时,复选标记将消失,但仍处于禁用状态。即使按下重置按钮,被禁用的箱子也会保留 Javascript: <script> function checkAllbase(bx) { var cbs = document.getElementsByTagName('input'); for(var i=0; i < cbs.length

我有几页很长的代码,但我很难让它正常工作。下面的脚本使用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> 

函数checkAllbase(bx){
var cbs=document.getElementsByTagName('input');
对于(变量i=0;i
元素保持禁用状态,因为设置的值是常量(文字)
true

您可以做的是在整个过程中复制
bx.disabled
,就像您使用
bx.checked所做的那样:

cbs[i].checked = bx.checked;
cbs[i].disabled = bx.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

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

HTML


JS

function-togglePackage(isChecked,packageName){
//从下面两个选项中选择一个进行使用
//下面按类名选择输入
var cbs=document.queryselectoral(“输入”。+packageName);
//或以下按输入名称选择输入
var cbs=document.queryselectoral(“输入[name^=“+packageName+”]”);

对于(var i=0;i“具有特定ID的所有复选框”ID对于整个文档中的单个元素应该是唯一的。它们不支持重用。
名称或可能更合适。我看不到您试图删除“无论如何都禁用”复选框。Jonathan,我对4组不同的复选框使用了4个不同的ID,因为它们分散在整个文件中,并且至少有100个复选框Patrick,我不知道如何使用这个函数。当我按下重置按钮时,它们都会不断取消选中,但禁用属性保持不变。@user2590331,
.disabled=false
.removeAttr(“disabled”)
@PatrickEvans好的,我可以在该函数中加一个else吗?或者你看到更好的选项了吗?
<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">
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");