| 1 |
<!doctype html> |
| 2 |
<html lang="en"> |
| 3 |
<head> |
| 4 |
<meta charset="utf-8"> |
| 5 |
<meta name="viewport" content="width=device-width, initial-scale=1"> |
| 6 |
<title>jQuery UI Autocomplete - Multiple values</title> |
| 7 |
<link rel="stylesheet" href="../../themes/base/all.css"> |
| 8 |
<link rel="stylesheet" href="../demos.css"> |
| 9 |
<script src="../../external/requirejs/require.js"></script> |
| 10 |
<script src="../bootstrap.js"> |
| 11 |
var availableTags = [ |
| 12 |
"ActionScript", |
| 13 |
"AppleScript", |
| 14 |
"Asp", |
| 15 |
"BASIC", |
| 16 |
"C", |
| 17 |
"C++", |
| 18 |
"Clojure", |
| 19 |
"COBOL", |
| 20 |
"ColdFusion", |
| 21 |
"Erlang", |
| 22 |
"Fortran", |
| 23 |
"Groovy", |
| 24 |
"Haskell", |
| 25 |
"Java", |
| 26 |
"JavaScript", |
| 27 |
"Lisp", |
| 28 |
"Perl", |
| 29 |
"PHP", |
| 30 |
"Python", |
| 31 |
"Ruby", |
| 32 |
"Scala", |
| 33 |
"Scheme" |
| 34 |
]; |
| 35 |
function split( val ) { |
| 36 |
return val.split( /,\s*/ ); |
| 37 |
} |
| 38 |
function extractLast( term ) { |
| 39 |
return split( term ).pop(); |
| 40 |
} |
| 41 |
|
| 42 |
$( "#tags" ) |
| 43 |
// don't navigate away from the field on tab when selecting an item |
| 44 |
.on( "keydown", function( event ) { |
| 45 |
if ( event.keyCode === $.ui.keyCode.TAB && |
| 46 |
$( this ).autocomplete( "instance" ).menu.active ) { |
| 47 |
event.preventDefault(); |
| 48 |
} |
| 49 |
}) |
| 50 |
.autocomplete({ |
| 51 |
minLength: 0, |
| 52 |
source: function( request, response ) { |
| 53 |
// delegate back to autocomplete, but extract the last term |
| 54 |
response( $.ui.autocomplete.filter( |
| 55 |
availableTags, extractLast( request.term ) ) ); |
| 56 |
}, |
| 57 |
focus: function() { |
| 58 |
// prevent value inserted on focus |
| 59 |
return false; |
| 60 |
}, |
| 61 |
select: function( event, ui ) { |
| 62 |
var terms = split( this.value ); |
| 63 |
// remove the current input |
| 64 |
terms.pop(); |
| 65 |
// add the selected item |
| 66 |
terms.push( ui.item.value ); |
| 67 |
// add placeholder to get the comma-and-space at the end |
| 68 |
terms.push( "" ); |
| 69 |
this.value = terms.join( ", " ); |
| 70 |
return false; |
| 71 |
} |
| 72 |
}); |
| 73 |
</script> |
| 74 |
</head> |
| 75 |
<body> |
| 76 |
|
| 77 |
<div class="ui-widget"> |
| 78 |
<label for="tags">Tag programming languages: </label> |
| 79 |
<input id="tags" size="50"> |
| 80 |
</div> |
| 81 |
|
| 82 |
<div class="demo-description"> |
| 83 |
<p>Usage: Type something, eg. "j" to see suggestions for tagging with programming languages. Select a value, then continue typing to add more.</p> |
| 84 |
<p>This is an example showing how to use the source-option along with some events to enable autocompleting multiple values into a single field.</p> |
| 85 |
</div> |
| 86 |
</body> |
| 87 |
</html> |