简体   繁体   English

JS if语句似乎不起作用

[英]JS if statement doesn't seem to work

I have a checkbox and if it is checked, I want text boxes to be editable. 我有一个复选框,如果选中,我希望文本框可编辑。 If it remains unchecked, then I want the text boxes to be readOnly. 如果未选中它,那么我希望文本框为readOnly。 Here is my JS: 这是我的JS:

if (document.getElementById('itemize').checked==false) {
    document.getElementById('budget_1').readOnly = true;
} else  {
    document.getElementById('budget_1').readOnly = false;
     }  

My HTML for the checkbox: 我的HTML复选框:

<input type="checkbox" id="itemize" style="width: 20px; height: 20px;" />

The JS seems to make the text box readOnly just fine, but when I make sure the checkbox is checked, the text box remains readOnly. JS似乎使文本框readOnly很好,但是当我确保选中复选框时,文本框仍为readOnly。 Any suggestions? 有什么建议么?

You would need to use an event handler to run when the checkbox changes state 当复选框更改状态时,您将需要使用事件处理程序来运行

  var budget_1 = document.getElementById('budget_1'), itemize = document.getElementById('itemize'); itemize.addEventListener("change", readonly); function readonly(){ if (itemize.checked==false) { budget_1.readOnly = true; } else { budget_1.readOnly = false; } } 
 <input type="checkbox" id="itemize"/> <input type="text" id="budget_1" readonly/> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM