繁体   English   中英

Javascript禁用另一个选择框选项上的选择框

[英]Javascript disable select box on another select box option

我有两个选择下拉列表:

<select name="internal_message">
<option value="yes">Yes</option>
<option value="" selected="selected">No</option>
</select>

<select name="internal_message_agent">
<option value="">Choose</option>.... etc
</select>

我努力了:

<select name="internal_message">
            <option value="yes" onClick="document.getElementById('internal_message_agent').disabled=!this.selected;">Yes</option>
            <option value="" selected="selected">No</option>
            </select>

但它不起作用 - 如何在internal_message选择值为"No"时禁用internal_message_agent select元素,并在所选选项为"Yes"时启用它

为每个选择添加一个ID并使用jquery来禁用它,如下所示:

$('#box1').on('change', function(){
if($(this).val()==='no'){
    $('#box2').attr('disabled', 'disabled');
}else{
    $('#box2').attr('disabled', false);
}
});

http://jsfiddle.net/3MCaG/1/

这是使用Javascript和HTML看起来的样子。

<!DOCTYPE html>
<html>
<head>
</head>
<script>
function validate(){
    var b1 = document.getElementById("box1");
    var f = b1.options[b1.selectedIndex].value;
    var b2 = document.getElementById("box2");
        if(f == "no"){
        b2.disabled="disabled";
        }
    else{
    b2.disabled="";
    }
}
</script>
<select id="box1" onchange="validate()">
    <option id="yes" value="yes">Yes</option>
    <option id="no" value="no" selected="selected">No</option>
</select>

<select id="box2" disabled>
    <option></option>
    <option value="">Choose</option>
</select>
</html>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM