| 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, remote</title> |
| 7 |
<link rel="stylesheet" href="../../themes/base/all.css"> |
| 8 |
<link rel="stylesheet" href="../demos.css"> |
| 9 |
<style> |
| 10 |
.ui-autocomplete-loading { |
| 11 |
background: white url("images/ui-anim_basic_16x16.gif") right center no-repeat; |
| 12 |
} |
| 13 |
</style> |
| 14 |
<script src="../../external/requirejs/require.js"></script> |
| 15 |
<script src="../bootstrap.js"> |
| 16 |
function split( val ) { |
| 17 |
return val.split( /,\s*/ ); |
| 18 |
} |
| 19 |
function extractLast( term ) { |
| 20 |
return split( term ).pop(); |
| 21 |
} |
| 22 |
|
| 23 |
$( "#birds" ) |
| 24 |
// don't navigate away from the field on tab when selecting an item |
| 25 |
.on( "keydown", function( event ) { |
| 26 |
if ( event.keyCode === $.ui.keyCode.TAB && |
| 27 |
$( this ).autocomplete( "instance" ).menu.active ) { |
| 28 |
event.preventDefault(); |
| 29 |
} |
| 30 |
}) |
| 31 |
.autocomplete({ |
| 32 |
source: function( request, response ) { |
| 33 |
$.getJSON( "search.php", { |
| 34 |
term: extractLast( request.term ) |
| 35 |
}, response ); |
| 36 |
}, |
| 37 |
search: function() { |
| 38 |
// custom minLength |
| 39 |
var term = extractLast( this.value ); |
| 40 |
if ( term.length < 2 ) { |
| 41 |
return false; |
| 42 |
} |
| 43 |
}, |
| 44 |
focus: function() { |
| 45 |
// prevent value inserted on focus |
| 46 |
return false; |
| 47 |
}, |
| 48 |
select: function( event, ui ) { |
| 49 |
var terms = split( this.value ); |
| 50 |
// remove the current input |
| 51 |
terms.pop(); |
| 52 |
// add the selected item |
| 53 |
terms.push( ui.item.value ); |
| 54 |
// add placeholder to get the comma-and-space at the end |
| 55 |
terms.push( "" ); |
| 56 |
this.value = terms.join( ", " ); |
| 57 |
return false; |
| 58 |
} |
| 59 |
}); |
| 60 |
</script> |
| 61 |
</head> |
| 62 |
<body> |
| 63 |
|
| 64 |
<div class="ui-widget"> |
| 65 |
<label for="birds">Birds: </label> |
| 66 |
<input id="birds" size="50"> |
| 67 |
</div> |
| 68 |
|
| 69 |
<div class="demo-description"> |
| 70 |
<p>Usage: Enter at least two characters to get bird name suggestions. Select a value to continue adding more names.</p> |
| 71 |
<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> |
| 72 |
</div> |
| 73 |
</body> |
| 74 |
</html> |