如何在下拉列表中启用多个条件?

时间:2015-05-28 08:15:05

标签: javascript php drop-down-menu

我有一个下拉列表。当我选择"其他" "其他操作文本框" 将启用。
当我选择"课堂培训" "在职培训(OJT)" 时,下拉" Ilsas" 和textbox "拟议的公共培训" 的拟议培训将启用。
对于"其他" 条件,我做得对。

然后,我如何在JavaScript中添加其他条件?这是因为我试图将条件组合到JavaScript中,但它没有成功。



<script language="javascript" type="text/javascript"> 
    function otherAction(val) {
        var otherAct = document.getElementById('otherAct');
        if(val=='Others') {
            otherAct.style.display = "block";
        } else {    
            otherAct.style.display = "none";
        }
    }
</script>
&#13;
<table>
    <tr>
        <td>Action: </td>
        <td>
            <select name="perAction" onchange="otherAction(this.value)"> 
                <option value="0"></option>                    
                <option value="Classroom Training">Classroom Training</option>
                <option value="Coaches and Mentoring by IM">Coaches and Mentoring by IM</option>
                <option value="On Job Training (OJT)">On Job Training (OJT)</option>
                <option value="Others">Others</option>
            </select>
        </td>
        <td>Other Action: </td>
        <td><input name="otherAct" type="text" id="otherAct" /></td>
        <td>Proposed Training in Ilsas: </td>
        <td>
            <select name = "perIlsas">
                <option value="0"></option>                    
                <option value="Project Management">Project Management</option>
                <option value="Contract Management">Contract Management</option>
                <option value="Analytical Skill">Analytical Skill</option>
            </select>
        </td>
        <td>Proposed Training in Public: </td>
        <td><input name="pubTraining" type="text" id="pubTraining" /></td>
    </tr>
</table>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

您只需使用多个if-else条件

<script language="javascript" type="text/javascript"> 
    function otherAction(val) {
        var otherAct = document.getElementById('otherAct');
        if(val=='Others') {
            otherAct.style.display = "block";
        } else if(val == 'On Job Training (OJT)'){    
             otherAct.style.display = "none";
             pubTraining.style.display = "block";
            // set display here either `block` or `none` according to your need
        }
        else{
             // set display here either block or none according to your need
        }
    }
</script>

修改 首先为该选择框提供一些id属性。然后,您可以像这样编写另一个else-if条件检查

<select id="perIlsas" name = "perIlsas"> //assigning id attribute

else if(val == 'Coaches and Mentoring by IM'){ //another else-if condition    
       $('#perIlsas').prop('disabled', 'disabled'); //this will disable entire selectbox
        }

您可以在jQuery Docs

中了解有关prop的更多信息