Javascript 从下拉菜单中选择的选项上启用/禁用文本框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5258182/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Enable/disable of textbox on option selected from drop down menu
提问by Nyaro
What I want is that the text box is only accessible if a certain option is picked from the drop down menu and I have an html form as below:
我想要的是只有在从下拉菜单中选择某个选项时才能访问文本框,并且我有一个 html 表单,如下所示:
<tr>
<td>a. Did any of your staff participate in training or orientation sessions related to any aspect of social performance management, during the reporting year? </td>
<td >
<p>
<select name="mfi_4_a_i">
<option>Yes</option>
<option>No</option>
<option>No, but planning in future</option>
</select>
</p>
<p>if not,and not planning please explain why not </p>
<input type="text" name="mfi_4_a_ii" id="sdd" />
</tr>
Now when a user selects the option No, but planning in future then the text box must be enabled otherwise the textbox must be disabled.
现在,当用户选择选项 No, but Planning in future 时,必须启用文本框,否则必须禁用文本框。
How do I accomplish this?
我该如何实现?
回答by Awais Qarni
You should call the javascript function for this.
您应该为此调用 javascript 函数。
<select id="mfi_4_a_i" name="mfi_4_a_i" onChange="changetextbox();">
<option>Yes</option>
<option>No</option>
<option>No, but planning in future</option>
</select>
<input type="text" name="mfi_4_a_ii" id="sdd" />
<script type="text/javascript">
function changetextbox()
{
if (document.getElementById("mfi_4_a_i").value === "noy") {
document.getElementById("sdd").disable='true';
} else {
document.getElementById("sdd").disable='false';
}
}
</script>
回答by Mahesh
For me the document.getElementById("sdd").disabled='false'
did not work so I used
对我来说document.getElementById("sdd").disabled='false'
没有用所以我用了
document.getElementById("sdd").disabled='';
document.getElementById("sdd").disabled='';
if (document.getElementById("mfi_4_a_i").value === "noy") {
document.getElementById("sdd").disabled='true';
} else {
document.getElementById("sdd").disabled='';
}
回答by Amoh Gil
Just corrections... removal of ; , === , noy , 'true' #no quotes
只是更正...删除; , === , noy , 'true' #无引号
onChange="changetextbox();"
if (document.getElementById("mfi_4_a_i").value == "no") {
document.getElementById("sdd").disabled=true;
回答by konsolenfreddy
Without doing a roundtrip to the server, you'll have to do that with javascript
无需对服务器进行往返,您必须使用 javascript 执行此操作
回答by Philip Wiggins
document.getElementById("the_id").disabled='';
works for me. works, much like style.display
document.getElementById("the_id").disabled='';
为我工作。作品,很像style.display