i am making form input with autocomplete multiple values and perfectly works, but i get some problem. i want if the first option I choose Alvin, then the second option I choose Yossy. so when I want to choose Yossy, option Alvin be there with a red background ( class active )
I tried to learn the method from jquery ui autocomplete, but I am still confused as to how it works.
this my code
$(document).ready(function() {
var availableTags = [
"Alvin Lim",
"Khia Wijaya",
"Suryo Guritno",
"Adjie Hidayat",
"Yossy Mo",
"Iwan Prasetyo",
];
function split( val ) {
return val.split( /,\s*/ );
}
function extractLast( term ) {
return split( term ).pop();
}
$( "#account" )
// don't navigate away from the field on tab when selecting an item
.bind( "keydown", function( event ) {
/* $('#keyword input#account').css('background','url(../image/arrow-up.png) no-repeat 95% center #bcbcbc'); */
if ( event.keyCode === $.ui.keyCode.TAB &&
$( this ).data( "ui-autocomplete" ).menu.active ) {
event.preventDefault();
}
})
.autocomplete({
minLength: 0,
source: function( request, response ) {
// delegate back to autocomplete, but extract the last term
response( $.ui.autocomplete.filter(
availableTags, extractLast( request.term ) ) );
},
focus: function() {
// prevent value inserted on focus
return false;
},
select: function( event, ui ) {
var terms = split( this.value );
// remove the current input
terms.pop();
// add the selected item
terms.push( ui.item.value );
// add placeholder to get the comma-and-space at the end
terms.push( "" );
this.value = terms.join( ", " );
return false;
}
});
});
anyone can help me? thanks in advance, regards :)
I have'nt checked it out mate,this is juz a work-around.There might be other correct ways of doing it.But i think something like this might help u mate :)
$(document).ready(function() {
var terms;
var availableTags = [
"Alvin Lim",
"Khia Wijaya",
"Suryo Guritno",
"Adjie Hidayat",
"Yossy Mo",
"Iwan Prasetyo",
];
function split( val ) {
return val.split( /,\s*/ );
}
function extractLast( term ) {
return split( term ).pop();
}
$( "#account" )
// don't navigate away from the field on tab when selecting an item
.bind( "keydown", function( event ) {
/* $('#keyword input#account').css('background','url(../image/arrow-up.png) no-repeat 95% center #bcbcbc'); */
if ( event.keyCode === $.ui.keyCode.TAB &&
$( this ).data( "ui-autocomplete" ).menu.active ) {
event.preventDefault();
}
})
.autocomplete({
minLength: 0,
source: function( request, response ) {
// delegate back to autocomplete, but extract the last term
response( $.ui.autocomplete.filter(
availableTags, extractLast( request.term ) ) );
},
focus: function() {
// prevent value inserted on focus
return false;
},
select: function( event, ui ) {
terms = split( this.value );
// remove the current input
terms.pop();
// add the selected item
terms.push( ui.item.value );
// add placeholder to get the comma-and-space at the end
terms.push( "" );
this.value = terms.join( ", " );
return false;
}
}).data('autocomplete')._renderItem = function( ul, item ) {
if($.inArray(item.label, terms) > -1 ){
return $( "<li></li>" )
.data( "item.autocomplete", item )
.append( '<a class="selected">' + item.label + '</a>' )
.appendTo( ul );
}else{
return $( "<li></li>" )
.data( "item.autocomplete", item )
.append( '<a>' + item.label + '</a>' )
.appendTo( ul );
}
};
//}
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句