启用/禁用从下拉菜单中选择的选项上的文本框

时间:2011-03-10 10:12:41

标签: javascript html

我想要的是,只有从下拉菜单中选择某个选项并且我有一个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>

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

我如何做到这一点?

5 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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='';
}

答案 2 :(得分:2)

只是改正...... 去除; ,===,noy,'true'#no quotes

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

答案 3 :(得分:0)

如果不对服务器进行往返,则必须使用javascript进行此操作

答案 4 :(得分:0)

document.getElementById("the_id").disabled='';适合我。与style.display

非常相似
相关问题