views:

1083

answers:

2

Hi,

I have a page with two input fields City and Venue. I have the autocomplete plugin from Devbridge working great for the city field. I now want to get it working on the venue field. The javascript I have so far is:

<script type="text/javascript">
    $(document).ready(function() {
        $('#Event_City').autocomplete({
          serviceUrl: '<%=Url.Action("GetCities", "Search") %>',
          minChars:2,
          width: 300,
          delimiter: /(,|;)\s*/,
          deferRequestBy: 150, //miliseconds
          params: { country: 'Yes' },
        });
        $('#Event_Venue').autocomplete({
          serviceUrl: '<%=Url.Action("GetVenues", "Search") %>',
          minChars:2,
          width: 300,
          delimiter: /(,|;)\s*/,
          deferRequestBy: 150, //miliseconds
          params: { city: $("#Event_City").val() },
        });
    });      
</script>

The second autocomplete passes an additional paramter (city) to the action on my controller. I will then use this to restrict my responses to venues in that city. This paramter is received but does not contain the current value entered in #Event_City. Instead it contains the default value.

Does anyone know how to evaluate the value when the autocomplete gets called?

I am just starting out with Javascript so please be gentle.

Thanks,

+3  A: 

The reason it contains the default value is because the autocomplete is attached to the textbox when the page is loaded. In order for it to be updated with the new value, the plugin would need to provide a way for you to update properties of it's parameter object or allow you to destroy it on the second element and then rebuild it every time the user changes the first one's value. The AutoComplete library you chose looks pretty lightweight (not always a bad thing... just in your case it is...) and doesn't seem to support this functionality.

My best suggestion for you would be to try another library. I tend to like this one very much as it's quite versatile (demo page):

http://view.jquery.com/trunk/plugins/autocomplete/demo/

KyleFarris
You bring up a very good point +1
ichiban
+7  A: 

@Ian Mckay - In order to get the AutoComplete for the Venue to filter based on the City, you need to bind in the "change" event of the City as follows:

$(document).ready(function() {
    $('#Event_City').autocomplete({
      serviceUrl: '',
      minChars:2,
      width: 300,
      delimiter: /(,|;)\s*/,
      deferRequestBy: 150, //miliseconds
      params: { country: 'Yes' },
    }).change(function(){
     $('#Event_Venue').autocomplete({
       serviceUrl: '',
       minChars:2,
       width: 300,
       delimiter: /(,|;)\s*/,
       deferRequestBy: 150, //miliseconds
       params: { city: $("#Event_City").val() }
     });
    }); 
});
Jose Basilio
Are you sure this isn't going to bind the autocomplete plugin to the textbox more than once? This could be bad. Like, if you change the first textbox's value, say, 20 times, will the second textbox be autosuggest-ified 20 times (and hence make 20 asynchronous ajax calls to the backend at one time)? o_0
KyleFarris
@Kyle - I believe, you are correct. This would happen, if the user decides to go back and change the city. The standard autocomplete jQuery plugin does have a "destroy" option that could be passed, but this one does not.
Jose Basilio