Reputation: 1018
I have implemented the autocomplete using JQuery, when I select the data, it gets stored in the result table, after selecting, I need to clear the autocomplete textbox which i'm not able to. Searched for the same and got this Clear text box in Jquery Autocomplete after selection, but i did not know where to place it and also in the firebug, I got error at function(event,ui). Pls help... my code is as follows.
$(function() {
function log( message ) {
$( "<div>" ).text( message ).prependTo( "#log" ).click(function(o){
$(this).remove();
});
$( "#log" ).scrollTop( 0 );
}
$( "#poolName" ).autocomplete({
source: function( request, response ) {
$.ajax({
url: "/DataWeb/getPoolName",
type : 'post',
dataType: 'json',
data: { name_startsWith: request.term },
success: function( data ) {
console.log(data);
response( $.map( data, function( item ) {
return {
label: item.poolName,
value: item.poolName
}
}));
}
});
},
minLength: 1,
select: function( event, ui ) {
log( ui.item ?
ui.item.label :
"Nothing selected, input was " + this.value);
},
open: function() {
$( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
},
close: function() {
$( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
}
});
});
Upvotes: 4
Views: 10003
Reputation: 4447
I found that I had to do both clear the field and return false
select: function(ev, ui) {
console.log(this, ui.item)
$(this).val('')
return false
},
Upvotes: 0
Reputation: 623
The problem with the other answers is that it didn't had the preventDefault call.
This will work:
select: function (e, i) {
e.preventDefault();
$('#poolName').val('');
return false;
}
Upvotes: 0
Reputation: 1018
I tried with this document.getElementById("poolName").value=""; and it is working.
function log( message ) {
document.getElementById("poolName").value="";
$( "<div>" ).text( message ).prependTo( "#log" ).click(function(o){
$(this).remove();
});
$( "#log" ).scrollTop( 0 );
}
Upvotes: 1
Reputation: 3175
$( "#poolName" ).autocomplete({
source: function( request, response ) {
$.ajax({
url: "/DataWeb/getPoolName",
type : 'post',
dataType: 'json',
data: { name_startsWith: request.term },
success: function( data ) {
console.log(data);
response( $.map( data, function( item ) {
return {
label: item.poolName,
value: item.poolName
}
}));
}
});
},
select: function (e, i) {
$('#poolName').val('');
return false;
}
,minLength: 1,
select: function( event, ui ) {
log( ui.item ?
ui.item.label :
"Nothing selected, input was " + this.value);
},
open: function() {
$( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
},
close: function() {
$( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
}
});
});
Upvotes: 3
Reputation: 5737
try using return false;
inside select: clause,
select: function( event, ui ) {
log( ui.item ?
ui.item.label :
"Nothing selected, input was " + this.value);
return false;
},
Upvotes: 2