简体   繁体   English

jQuery禁用选定的选项

[英]Jquery disable selected options

I have tried to apply this fiddle solution but without any luck :( 我试图应用这种小提琴解决方案,但没有任何运气:(

$('.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);
    });
});

I want to make disabled all selected items from previously selected lists. 我想禁用先前选定列表中的所有选定项目。

That is not working because you are adding the select lists dynamically and to use the keyup and change events, you need to use delegation: 那是行不通的,因为要动态添加选择列表,并且要使用keyup和change事件,需要使用委托:

$(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);
    });
});

See the fiddle: https://jsfiddle.net/wfaaztnb/8/ 看到小提琴: https : //jsfiddle.net/wfaaztnb/8/

Just replace the selector $('.select') with $(document) and that's it! 只需将选择器$('.select')替换$(document)就可以了! :) :)

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

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