Не выводить элементы с атрибутом disabled

Рейтинг: 0Ответов: 1Опубликовано: 29.04.2015

Есть скрипт, ищет по введённому слову совпадения в списке select. Нужно сделать чтобы в найденные результаты не включались элементы с атрибутом disabled. Сам код:

jQuery.fn.filterByText = function(textbox, selectSingleMatch) {
    return this.each(function() {
        var select = this;
        var options = [];
        $(select).find('option').each(function() {
            options.push({
                value: $(this).val(),
                text: $(this).text()
            });
        });
        $(select).data('options', options);
        $(textbox).bind('change keyup', function() {
            var options = $(select).empty().scrollTop(0).data('options');
            var search = $.trim($(this).val());
            var regex = new RegExp(search, 'gi');
            $.each(options, function(i) {
                var option = options[i];
                if (option.text.match(regex) !== null) {
                    $(select).append(
                        $('<option>').text(option.text).val(option.value)
                    );
                }
            });
            if (selectSingleMatch === true &&
                $(select).children().length === 1) {
                $(select).children().get(0).selected = true;
            }
        });
    });
};

$(function() {
    $('#select').filterByText($('#text'), true);
});

Ответы

▲ 1Принят
// Замените эту часть кода
$(select).find('option')
// на эту
$(select).find('option:not(:disabled)')

UPD

;(function ($) {
    $.fn.filterByText = function (opt) {

        opt = $.extend({
            input: null,
            byVal: true,
            byTxt: true,
            allowDisable: false,
            singleMatch: false
        }, opt);

        var filtering = function () {
            if (!opt.input) return false;
            var $that = $(this),
                cache = $('option', $that);
            $(document).on('input', opt.input, function () {
                var search = $.trim($(this).val()),
                    re = new RegExp(search, 'gi');
                if (search == '') $that.html(cache).prop('selectedIndex', 0);
                if (!opt.singleMatch && search.length <= 1) return false;
                $that.empty();
                cache.filter(function () {
                    if ($(this).is(':disabled') && !opt.allowDisable) return false;
                    return (opt.byVal && $(this).val().match(re) || opt.byTxt && $(this).text().match(re));
                }).appendTo($that);

            });
        };
        return this.each(filtering);
    };
})(jQuery);
// пример использования
$('#select').filterByText({
    input: '#text', // селектор поля ввода поисковой фразы
    byVal: true, // по текстовому описанию, true/false
    byTxt: true, // по значению option, true/false
    singleMatch: false, // начинать поиск с первого символа, true/false
    allowDisable: true // учитывать option:disabled, true/false
});