views:

60

answers:

2

Disclaimer: I have gone through the related questions, and could not find solutions to this particular issue.

Scenario is this:

Based 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

+1  A: 

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.
         }
       }

     }
   }
});
Keyo
I have the change event hooked up, and for testing, I put an alert box in there. Does not seem to have any effect :(
Hopefully search will work for you. Basically I just put an if statement in the ajax object. You could probably do the same if you use $.ajax in the search option.
Keyo
Search worked like a charm - thanks for your help.
A: 

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.

Protron
My select looks like this:select: function (event, ui) { var normalText = ui.item ? ui.item.label.replace(/(<.+?>)/gi, '') : $(this).value; var myTargetUrl = "CreateNew/CreateNew?newTitle=" + normalText; $('#<%= id %>').val(normalText); $.ajax({ type: 'POST', url: myTargetUrl }); alert(myTargetUrl); return false; }But the alert dialog never comes up, when I enter a value that does not exist and press <ENTER>At what point do I have access to the entered value, I have tried search event, change event etc to no avail
I don't know why the alert does not appear. Maybe the ajax call is throwing an exception. But I made a sample here and it does appear http://jsbin.com/aqewu4/edit (even when the ajax call points to nothing). Maybe that sample page can help you work out the problem. If not, edit your question with more details or create a new question.
Protron