如果勾选复选框,如何在复选框中启用输入?当我选中输入类型

时间:2017-10-20 02:39:26

标签: javascript html

如果选中复选框,如何在复选框中启用输入?

当我选中输入类型的复选框时,它总是禁用。

function ckChange(ckType) {
  var ckName = document.getElementsByName(ckType.name);
  var checked = document.getElementById(ckType.id);

  if (checked.checked) {
    for (var i = 0; i < ckName.length; i++) {

      if (!ckName[i].checked) {
        ckName[i].disabled = true;
      } else {
        ckName[i].disabled = false;
      }
    }
  } else {
    for (var i = 0; i < ckName.length; i++) {
      ckName[i].disabled = false;
    }
  }
}
<input type="checkbox" placeholder="toBeReturn" name="progress" id="progress1" value="1" tabIndex="1" onClick="ckChange(this)"> For use in showroom to be return on
<input type="date" name="progress" id="progress3" placeholder="date">
<br/>
<input type="checkbox" placeholder="OnLoan" name="progress" id="progress2" value="1" tabIndex="1" onClick="ckChange(this)"> On Loan
<br/>
<input type="checkbox" placeholder="Other" name="progress" id="progress6" value="1" tabIndex="1" onClick="ckChange(this)"> Other
<input type="text" name="progress" id="progress3" placeholder="State the Purpose">

我需要帮助来完成我的项目谢谢!

2 个答案:

答案 0 :(得分:0)

只需切换你所拥有的&#34;如果&#34;和&#34;否则&#34;声明,以便相反的情况发生。

另外,如果您在未选中复选框的情况下加载页面,也请添加&#34;禁用&#34;到文本输入所以匹配。

&#13;
&#13;
function ckChange(ckType) {
  var ckName = document.getElementsByName(ckType.name);
  var checked = document.getElementById(ckType.id);

  if (checked.checked) {
    for (var i = 0; i < ckName.length; i++) {
      ckName[i].disabled = false;
    }
  } else {

    for (var i = 0; i < ckName.length; i++) {
      if (!ckName[i].checked) {
        ckName[i].disabled = true;
      } else {
        ckName[i].disabled = false;
      }
    }
  }
}
&#13;
<th>
  <input type="checkbox" placeholder="toBeReturn" name="progress" id="progress3" value="1" tabIndex="1" onClick="ckChange(this)"> For use in showroom to be return on <input type="date" name="progress" id="progress3" placeholder="date" disabled>
</th>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

我认为问题在于你的'&#39; for&#39;声明,&#39; for&#39;声明匹配所有复选框,包括您检查过的那个复选框,因此您可以这样尝试:

function ckChange(ckType) {
        var ckName = document.getElementsByName(ckType.name);
        var checked = document.getElementById(ckType.id);

        if (checked.checked) {
            for (var i = 0; i < ckName.length; i++) {
                if(ckName[id].id !==checked.id){
                    ckName[i].disabled = false;
                }
            }
        } else {
            for (var i = 0; i < ckName.length; i++) {
                if(ckName[id].id !==checked.id){
                    if (!ckName[i].checked) {
                    ckName[i].disabled = true;
                    } else {
                        ckName[i].disabled = false;
                    }
                } 
            }
        }
    }