-1

I have this search function for a multiple select:

var SWAPLIST = {};
SWAPLIST.search = function(list, search) {
  $(list)
    .children()
    .prop('selected', false)
    .filter(function() {
       if (!search) 
       {
         return false;
       }
       return $(this)
       .text()
       .toLowerCase()
       .indexOf(search.toLowerCase()) > - 1;
    })
   .prop('selected', true)
};

Bound it to this event:

$(document).ready(function(){
   $('.entitySearch').keyup(function() {
     var kind    = $(this).attr('kind');
     var left  = '.leftEntities[kind=' + kind +']';
     var right = '.rightEntities[kind='+ kind +']';

     SWAPLIST.search(left + "," + right, $(this).val());
   });
});

This is an example of my multiselect:

<select class="leftEntities grid5" kind="<?php echo $firstKeyLeft;?>" multiple="multiple" size="10">     
  <option> a </option>
  <option> ab </option>
  <option> abc </option>
  <option> abcd </option>    
</select>   

This is my search input:

<div class="grid6 marginTop10px">
    <input kind="<?php echo $firstKeyLeft;?>" class="entitySearch form-control-static" role="form" type="text" size="25"/>
    <span class="glyphicon glyphicon-search"></span>
    <label> Suchen </label>
</div>           

And now I want to hide the options that don't match when I'm typing in the search key and of course unhide them when I remove or edit (so that it will match) the typed in search key.

Here the original relevant php and html code: http://codepad.org/4CXgkiei

4

1 回答 1

1

我修改了您的示例标记并编写了一些新的 JS 以使其工作。

在这里查看我的工作示例:http: //jsfiddle.net/7ZETF/2/

这是示例中的 JS:

$(function () {
    $('input[data-filter="example_select"]').on('keyup', function () {
        var $select_to_filter = $('[name="' + $(this).attr('data-filter') + '"]');
        var $move_hidden_options_to = $('select.hidden_options[for="' + $(this).attr('data-filter') + '"]');
        var filter_value = $(this).val();
        $select_to_filter.find('option').each(function () {
            if ($(this).text().indexOf(filter_value) == -1) {
                $(this).prependTo($move_hidden_options_to);
            }
        });
        $move_hidden_options_to.find('option').each(function () {
            if ($(this).text().indexOf(filter_value) !== -1) {
                $(this).prependTo($select_to_filter);
            }
        });
    });
});
于 2013-10-03T16:11:01.497 回答