I use JQuery plugin autocomplete as a kind of dataset chooser. If the user chooses a value from the autocomplete lookup, the database is queried for the matching dataset. If the user types in a new value, the user can enter a new dataset. An issue arises when the user types in an existing value rather than choosing it from the开发者_StackOverflow autocomplete lookup. When this is done, the autocomplete .result()
method is not called and no dataset is retrieved. To fix this I added a .blur(function(){$(this).search();});
to the input element. This fixed the original problem.
Now I have the problem that .result()
fires on selection from lookup AND on blur. I would like .result()
to fire on selection from lookup OR on blur. How do I make that happen?
Here is my code:
$('#groupset').autocomplete('ajax/php/leeruns.php');
$('#groupset').result(
function(event, data, formatted) {
if(data){
$('#groupsetdesc').val(formatted);
groups.load(data[1]); //retrieve matching dataset
} else {
$('#groupsetdesc').val('');
}
}
).blur(function(){$(this).search();});
You could store the current value of data in a persistent variable called previous and not load the dataset if it is the same.
var loaded_previous;
$('#groupset').autocomplete('ajax/php/leeruns.php');
$('#groupset').result(
function(event, data, formatted) {
if(data && data[1] != loaded_previous){
loaded_previous = data[1];
$('#groupsetdesc').val(formatted);
groups.load(data[1]); //retrieve matching dataset
} else {
$('#groupsetdesc').val('');
}
}
).blur(function(){$(this).search();});
精彩评论