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

Nya*_*aro 5 html javascript

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

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

我该如何做到这一点?

Awa*_*rni 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>
Run Code Online (Sandbox Code Playgroud)

  • .disable不是有效的属性/属性document.getElementById("sdd").disable ='true'; 应该是document.getElementById("sdd").disabled ='true'; 那么false不是"禁用"的有效值,而是"空". (2认同)

小智 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='';
}
Run Code Online (Sandbox Code Playgroud)