0

我正在使用 jquery multiselct 插件https://plugins.jquery.com/multi-select/

在一种情况下,我需要选择一些默认值,然后禁用它们,以便没有人可以删除它们。下面是我的代码,我根据某些条件选择了一些选项。

  $('#members_select3').find('option').remove().end()
    $.each(response.results, function (i, item) {
                        $('#members_select3').append($('<option>', { 
                            selected: function(){
                                if (**condition comes here**){
                                        return true;
                            },
                            value: item.uid,
                            text : item.contact,
                        }));
                    });

  enable_multi_select_search('members_select3');

现在根据文档,如果我在上面的代码中使用禁用选项,该选项将变为禁用但它不再被选中,这意味着该选项可供选择而不是默认选中。

$('#members_select3').find('option').remove().end()
$.each(response.results, function (i, item) {
    $('#members_select3').append($('<option>', { 
        selected: function(){
            if (**condition comes here**){
                    return true;
            }
        },
        value: item.uid,
        text : item.contact,
        disabled: function(){
            if (**condition comes here**){
                    return true;
            }
        }
    }));
});

enable_multi_select_search('members_select3');

插件n的默认功能

function enable_multi_select_search(id_){
    // refresh previous multi select box
    $('#members_select3').multiSelect("destroy");

    // create new
    $('#'+id_).multiSelect({
        selectableHeader: "<input type='text' class='form-control search-input' autocomplete='off' placeholder='search...'>",
        selectionHeader: "<input type='text' class='form-control search-input' autocomplete='off' placeholder='search...'>",
        afterInit: function (ms) {
            var that = this,
                $selectableSearch = that.$selectableUl.prev(),
                $selectionSearch = that.$selectionUl.prev(),
                selectableSearchString = '#' + that.$container.attr('id') + ' .ms-elem-selectable:not(.ms-selected)',
                selectionSearchString = '#' + that.$container.attr('id') + ' .ms-elem-selection.ms-selected';

            that.qs1 = $selectableSearch.quicksearch(selectableSearchString)
                .on('keydown', function (e) {
                    if (e.which === 40) {
                        that.$selectableUl.focus();
                        return false;
                    }
                });

            that.qs2 = $selectionSearch.quicksearch(selectionSearchString)
                .on('keydown', function (e) {
                    if (e.which == 40) {
                        that.$selectionUl.focus();
                        return false;
                    }
                });
        },
        afterSelect: function () {
            this.qs1.cache();
            this.qs2.cache();
        },
        afterDeselect: function () {
            this.qs1.cache();
            this.qs2.cache();
        }
    });
}
4

1 回答 1

0

你应该区分disabledreadOnly

  • disabled:该选项不可选择,即使不是默认选项
  • readOnly: 选项的状态(选择与否)不能改变

我猜,你要去第二个。

编辑

也许您可以像这样更改插件(未测试):

       $('#members_select3').append($('<option>', { 
                selected: function(){
                    if (**condition comes here**){
                            return true;
                    }
                },
                value: item.uid,
                text : item.contact,
                disabled: false,
                readOnly: function(){
                    if (**condition comes here**){
                            return true;
                    }
                }
            }));
        });
于 2016-01-05T15:00:25.130 回答