如果一个输入框可以接受多个输入项,比如选择你喜欢的语言,以逗号隔开,这是也可以使用AutoComplete为每个输入项提供输入提示。不过此时除了设置数据源外,还需要添加一些事件处理。
5 | <title>jQuery UI Demos</title> |
6 | <linkrel="stylesheet"href="themes/trontastic/jquery-ui.css"/> |
7 | <scriptsrc="scripts/jquery-1.9.1.js"></script> |
8 | <scriptsrc="scripts/jquery-ui-1.10.1.custom.js"></script> |
37 | return val.split(/,\s*/); |
39 | function extractLast(term) { |
40 | return split(term).pop(); |
44 | // don't navigate away from the field on tab |
45 | //when selecting an item |
46 | .bind("keydown", function (event) { |
47 | if (event.keyCode === $.ui.keyCode.TAB && |
48 | $(this).data("ui-autocomplete").menu.active) { |
49 | event.preventDefault(); |
54 | source: function (request, response) { |
55 | // delegate back to autocomplete, |
56 | // but extract the last term |
57 | response($.ui.autocomplete.filter( |
58 | availableTags, extractLast(request.term))); |
61 | // prevent value inserted on focus |
64 | select: function (event, ui) { |
65 | var terms = split(this.value); |
66 | // remove the current input |
68 | // add the selected item |
69 | terms.push(ui.item.value); |
70 | // add placeholder to get the |
71 | //comma-and-space at the end |
73 | this.value = terms.join(", "); |
81 | <divclass="ui-widget"> |
82 | <labelfor="tags">Tag programming languages: </label> |
83 | <inputid="tags"size="50"/> |
