2011-03-10 77 views
5

我想的是,如果某个选项,从下拉菜单中选择了文本框时,才可访问,我有如下的HTML表单:启用选项文本选自/禁用下拉菜单

<tr> 
<td>a.&nbsp;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> 

现在,当用户选择否选项,但在将来计划时,必须启用文本框,否则必须禁用文本框。

我该如何做到这一点?

+0

重复[问题](http://stackoverflow.com/questions/5255969/how-to-make-a-checkbox-enable-and-禁用一个文本框在多个案例)由同一用户 – diEcho 2011-03-10 10:27:21

回答

9

您应该为此调用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> 
+2

.disable不是有效的属性/属性 document.getElementById(“sdd”)。disable ='true'; 实际上应该是 document.getElementById(“sdd”)。disabled ='true'; 然后false不是“disabled”的有效值使它变为空而是。 – Nashe 2015-06-12 17:31:40

+0

什么是“noy”以及它代表最后一个选项 – 2017-07-25 05:17:30

0

如果不进行服务器往返,您必须使用javascript来做到这一点。

+0

可以告诉我详细或你能给我一个链接或一些提示做到这一点。 – Nyaro 2011-03-10 10:30:34

8

对我来说,document.getElementById("sdd").disabled='false'没有工作,所以我用

document.getElementById("sdd").disabled='';

if (document.getElementById("mfi_4_a_i").value === "noy") { 
    document.getElementById("sdd").disabled='true'; 
} else { 
    document.getElementById("sdd").disabled=''; 
} 
0

document.getElementById("the_id").disabled='';对我的作品。作品,很像style.display

2

只是更正... 删除; ,===,诺伊, '真' #NO报价

onChange="changetextbox();" 
if (document.getElementById("mfi_4_a_i").value == "no") { 
document.getElementById("sdd").disabled=true;