2017-01-03 62 views
2

我無法檢查選定的下拉選項是否被禁用。檢查所選下拉的道具是否被禁用

用戶可以選擇一個選項,然後選擇一個時間範圍,在時間選擇後,該範圍內所有不可用的選項將被設置爲禁用。如果先前選定的值也被禁用,則必須有警報。

我在想是這樣的:

if($('#dropdown').val().prop('disabled',true)){ 
alert('not possible'); 
} 
+1

你可以在'select'框中選擇一個禁用的選項嗎? – philantrovert

+0

是的,因爲該選項僅在用戶輸入日期範圍後才被禁用。第一個答案爲我工作 – nogato

回答

-1

你可以得到:selected選項,然後檢查其prop()

if($('#dropdown option:selected').prop('disabled') == true){ 
    //Selected option is disabled 
} 
+0

非常感謝! – nogato

1

使用此:

if($('#dropdown').find(':selected').prop('disabled')==true){ 
    alert('not possible'); 
} 

$('input').change(function(){ 
 
    if($(this).val()>50){ 
 
    $('select option:first-child').prop('disabled',true); 
 
    } 
 
    if($('select').find(':selected').prop('disabled')==true){ 
 
     alert('not possible'); 
 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<select> 
 
    <option value="1">1</option> 
 
    <option value="2">2</option> 
 
    <option value="3">3</option> 
 
</select> 
 
<input type="range"/>

+0

非常感謝! – nogato

+0

@ nogato,不要忘記投票或接受一個意見!請看看這裏:http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work,以幫助其他人。 –

+0

是的,我不會。我不能投票,因爲我缺乏聲譽,有一段時間,直到我可以接受答案:-) – nogato

0

檢查選定和禁用選項的計數。使用:selected:disabled僞類選擇器來選擇選項。

if($('#dropdown option:selected:disabled').length){ 
    alert('not possible'); 
}