JQuery autocomplete event at a new value
I have the following code:
container.autocomplete({
minLength: 2,
source: function(request, response) {
$.ajax({
type : 'POST',
url: HOST + "/lib/gets/getjsonclients.php",
dataType: "json",
data: {
q: request.term
},
success: function(data) {
ajaxCache[cachedTerm] = data;
response($.map(data, function(item) {
开发者_如何转开发 return {
label: item.value,
value: item.value
}
}));
}
});
},
select: function(event, ui) {
alert(ui.item.value);
this.close
},
//change: function(event, ui) {
// alert($(this).val());
//},
open: function() {
},
close: function() {
}
});
I want to catch the enter key event when I type a new value (other that which I already have in my select list).
I have tried using the .result event but I have an error: Uncaught TypeError: Object # has no method 'result' using the code:
container.result(function(event, data, formatted) {
alert($(this).val());
});
The change: event is working OK but is fired only after I loose the focus on autocomplete and I need it to be done when I hit the enter key
Thank you for your answers.
You should bind container to keypress and catch the enter key (not tested but should work):
$(container).keypress(function(event) {
if(event.which == 13) {
$(this).autocomplete('search');
}
});
I use this solution. However if there is a way to cancel the effect of one event while one was fired, and the activate again the event, please let me know.
var oneEventFired = false;
container.autocomplete({
minLength: 2,
source: function(request, response) {
$.ajax({
type : 'POST',
url: HOST + "/lib/gets/getjsonclients.php",
dataType: "json",
data: {
q: request.term
},
success: function(data) {
ajaxCache[cachedTerm] = data;
response($.map(data, function(item) {
return {
label: item.value,
value: item.value
}
}));
}
});
},
select: function(event, ui) {
oneEventFired = true;
alert(ui.item.value);
this.close
},
open: function() {
},
close: function() {
}
})
.keypress(function(e) {
if (!oneEventFired) {
// activate when the enter key is hit
if (e.keyCode === 13)
{
alert("key" + $(this).val());
}
} else {
oneEventFired = false;
}
});
Thank you for your help!
精彩评论