2016-04-25 45 views
0

應用此fiddle解決方案,但沒有任何運氣:(我試圖jQuery的禁用所選選項

$('.select').on('keyup change', function() { 
    var selected = []; 
    $(".select option:selected").each(function() { 
     if ($(this).val() !== "") selected.push($(this).val()); 
    }); 
    $(".select:not(this) option").each(function() { 
     if (selected.indexOf($(this).val()) > -1) $(this).prop("disabled", true); 
     else $(this).prop("disabled", false); 
    }); 
}); 

我想使殘疾人從先前選擇列表中的所有選定的項目。

+0

如果有人知道如何在添加新人員時禁用所選項目? – lewis4u

+0

我解決了我的問題....只是將第一行更改爲:$(document).on('click','.select',function(){ – lewis4u

回答

1

即不工作,因爲要動態添加的選擇列表,並使用KEYUP和改變事件,你需要使用委派:

$(document).on('keyup change', '.select', function() { 
    var selected = []; 
    $(".select option:selected").each(function() { 
     if ($(this).val() !== "") selected.push($(this).val()); 
    }); 
    $(".select:not(this) option").each(function() { 
     if (selected.indexOf($(this).val()) > -1) $(this).prop("disabled", true); 
     else $(this).prop("disabled", false); 
    }); 
}); 

見小提琴: https://jsfiddle.net/wfaaztnb/8/

+0

再次這個....現在它的作品:)但!如果用戶添加新的個人,他可以再次選擇已經選擇的個人...如何禁用? – lewis4u

+0

讓我更新代碼 –

+0

畢竟它不是那麼簡單:( – lewis4u

1

只需用$(document)替換選擇器$('.select')就是這樣! :)