小编Ara*_*hya的帖子

在Jquery中选择按搜索文本

目前我有代码来过滤选择的文本,如下所示.

图片 图片

当我在搜索框中输入文本时,它正在过滤.但是我需要在文本框中搜索选项而不进行过滤,如下所示.

图片

<script src="./jquery.min.js"></script>
<script>
    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().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() …
Run Code Online (Sandbox Code Playgroud)

html jquery select

4
推荐指数
1
解决办法
1453
查看次数

标签 统计

html ×1

jquery ×1

select ×1