Line | |
---|
1 | jQuery.fn.filterByText = function(textbox, selectSingleMatch) { |
---|
2 | return this.each(function() { |
---|
3 | var select = this; |
---|
4 | var options = []; |
---|
5 | $(select).find('option').each(function() { |
---|
6 | options.push({value: $(this).val(), text: $(this).text()}); |
---|
7 | }); |
---|
8 | $(select).data('options', options); |
---|
9 | $(textbox).bind('change keyup', function() { |
---|
10 | var options = $(select).empty().scrollTop(0).data('options'); |
---|
11 | var search = $.trim($(this).val()); |
---|
12 | var regex = new RegExp(search,'gi'); |
---|
13 | |
---|
14 | $.each(options, function(i) { |
---|
15 | var option = options[i]; |
---|
16 | if(option.text.match(regex) !== null) { |
---|
17 | $(select).append( |
---|
18 | $('<option>').text(option.text).val(option.value) |
---|
19 | ); |
---|
20 | } |
---|
21 | }); |
---|
22 | if (selectSingleMatch === true && |
---|
23 | $(select).children().length === 1) { |
---|
24 | $(select).children().get(0).selected = true; |
---|
25 | } |
---|
26 | }); |
---|
27 | }); |
---|
28 | }; |
---|
29 | |
---|
30 | |
---|
31 | $(function() { |
---|
32 | $('#filteredselect').filterByText($('#filterbox'), true); |
---|
33 | }); |
---|
Note: See
TracBrowser for help on using the repository browser.