Disclaimer: I have gone through the related questions, and could not find solutions to this particular issue.
Scenario is this:
Ba开发者_运维技巧sed on whether the user selected a suggestion from the dropdown or if there are no matches, I want to execute different jQuery ajax actions. How do I do this? The place where I am stuck is how to capture the input that is currently in the autocomplete input text box?
Thanks
To be honest I'm not sure. Looking at the documentation the search event should work.
Edit It really helps to read documentation. :D
Search method.
Triggers a search event, which, when data is available, then will display the suggestions; can be used by a selectbox-like button to open the suggestions when clicked. If no value argument is specified, the current input's value is used. Can be called with an empty string and minLength: 0 to display all items.
$( ".selector" ).autocomplete({
search: function(event, ui) {
$.ajax{
//Your ajax parameters...
success: function(data) { //No idea what format your data is in...
if(data['status'] == false) { //there is no result
//return your data.
//Trigger the events you want if the item does no exist.
}
else if(data['status'] == true){
//return data normally.
}
}
}
}
});
To capture the input that is currently in the autocomplete input text box you could use the select
method as in the autocomplete remote example:
$("#birds").autocomplete({
source: "search.php",
minLength: 2,
select: function(event, ui) {
log(ui.item ? ("Selected: " + ui.item.value + " aka " + ui.item.id) : "Nothing selected, input was " + this.value);
}
});
If ui.item
has a value, it means that something has matched (and the matched value is in there).
And if you want what the user has typed then use this.value
.
精彩评论