|
9 | 9 |
|
10 | 10 | <script type="text/javascript" src="jquery-1.3.1.min.js"></script> |
11 | 11 | <script type="text/javascript" src="jquery.autocomplete.js"></script> |
| 12 | + <script type="text/javascript" src="jquery.select-autocomplete.js"></script> |
12 | 13 | <script type="text/javascript"> |
13 | 14 | $(document).ready(function() { |
14 | | - init_autocomplete_selects(); |
| 15 | + $('select.autocomplete').select_autocomplete(); |
15 | 16 | }); |
16 | | - |
17 | | - function init_autocomplete_selects() { |
18 | | - //iterate over each autocomplete select |
19 | | - $('select.autocomplete').each(function() { |
20 | | - //stick each of it's options in to an items array of objects with name and value attributes |
21 | | - var select = this; |
22 | | - var items = []; |
23 | | - $(select).children('option').each(function(){ |
24 | | - var item = $(this); |
25 | | - if (item.val() != '') //ignore empty value options |
26 | | - { |
27 | | - var name = item.html(); |
28 | | - var value = item.val(); |
29 | | - items.push( {'name':name, 'value':value} ); |
30 | | - } |
31 | | - }); |
32 | | - |
33 | | - //make a new input box next to the select list |
34 | | - var input = $("<input type='text' />").appendTo('body'); |
35 | | - $(select).after(input); |
36 | | - |
37 | | - //make the input box into an autocomplete for the select items |
38 | | - $(input).autocomplete(items, { |
39 | | - data: items, |
40 | | - minChars: 0, |
41 | | - width: 310, |
42 | | - matchContains: false, |
43 | | - autoFill: false, |
44 | | - formatItem: function(row, i, max) { |
45 | | - return row.name; |
46 | | - }, |
47 | | - formatMatch: function(row, i, max) { |
48 | | - return row.name; |
49 | | - }, |
50 | | - formatResult: function(row) { |
51 | | - return row.name; |
52 | | - } |
53 | | - }); |
54 | | - |
55 | | - //make the result handler set the selected item in the select list |
56 | | - $(input).result(function(event, selected_item, formatted) { |
57 | | - var to_be_selected = $(select).find("option[value=" + selected_item.value + "]")[0]; |
58 | | - $(to_be_selected).attr('selected', 'selected'); |
59 | | - }); |
60 | | - |
61 | | - //set the initial text value of the autocomplete input box to the text node of the selected item in the select control |
62 | | - $(input).val($(select).find(':selected').text()); |
63 | | - |
64 | | - //normally, you'd hide the select list but we won't for this demo |
65 | | - //$(select).hide(); |
66 | | - }); |
67 | | - } |
| 17 | + |
68 | 18 | </script> |
69 | 19 | </head> |
70 | 20 |
|
|
0 commit comments