2012-03-28 66 views
1
How to show the dropdown Country blank when the State dropdown option is selected and also viceversa(country dropdown is selected and the state dropdown is blank 

我們是否需要在選項中包含選項空白或者是否有任何語法, 我已通過禁用選項完成,當我選擇國家/地區下拉菜單時,禁用狀態下拉菜單。 jQuery的選擇/下拉框示例如何選擇otehr下拉選項中的某個選項時顯示下拉空白?

<script type="text/javascript" src="js/jquery.js"></script> 

</head> 

<body> 

<h1>jQuery select/dropdown box example</h1> 

<script type="text/javascript"> 

    $(document).ready(function(){ 

    $("#country").change(function() { 


    // disable the dropdown: 

if($("#country option:selected").val()=="NONE"){ 
    $('#state').attr('enabled','true'); 
}else { 
    $('#state').attr('disabled','true'); 
} 


    }); 

    }); 
</script> 
</head><body> 

<select id="country"> 
<option value="NONE">-- Select --</option> 
<option value="China">China</option> 
<option value="United State">United State</option> 
<option value="Malaysia">Malaysia</option> 
</select> 
<select id ="state"> 
<option value ="AP">AP</option> 
<option value ="SP">SP</select> 
</body> 
</html> 

回答

0

只需使用removeAttr()方法。例如:

if($("#country option:selected").val()=="NONE"){ 
    $('#state').removeAttr('disabled'); 
}else { 
    $('#state').attr('disabled', 'disabled'); 
}