Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Autocomplete using extraParams to pass additional GET variables

I am referring specifically to the jQuery Autocomplete v1.1 plugin by Jörn Zaefferer [source: http://bassistance.de/jquery-plugins/jquery-plugin-autocomplete/] as there seems to be quite a few variations of this plugin.

I'm trying to pass additional parameters to the server when the user starts typing because I have multiple fields that I want autocomplete to provide suggestions for.

In addition to the query, I want to send the input name attribute to the server but I can't seem to use $(this).attr('name') within the extraParams.

My jQuery:

   $('.ajax-auto input').autocomplete('search.php', {      extraParams: {       search_type: function(){        return $(this).attr('name');       }      }    }) 

This is my HTML.

 <form method="post" action="#" id="update-form" autocomplete="off">   <ol>          <li class="ajax-auto">              <label for="form-initials">Initials</label>                 <input type="text" id="form-initials" name="initials" />             </li>          <li class="ajax-auto">              <label for="form-company">Company</label>                 <input type="text" id="form-company" name="company" />             </li>   </ol>  </form> 

Any suggestions?

like image 839
paperclip Avatar asked Apr 28 '10 08:04

paperclip


People also ask

How to pass extra parameter in jQuery autocomplete?

In that case, you need to define a callback function to the source option of autoComplete call. Also, you can pass multiple parameters using this approach. Your jQuery UI Autocomplete function would be like the following. In the source file, you can get the term and additional parameters using the $_GET variable.

How does autocomplete work in jQuery?

In the process of searching a specific value, the jQuery UI autocomplete selection feature provides the user with some string suggestions for the input area which effectively saves time. The autocomplete select action is triggered when the user selects one of the options from the pre-populated list.

How can create autocomplete search box in jQuery?

Syntax: $("TagId"). autocomplete({ source : itemList // List of Words. })


2 Answers

I am using the autocomplete function that is now part of jquery ui. Passing an 'extraParams' field does not work but you can just append the values in the request query string.

$(document).ready(function() {     src = 'http://domain.com/index.php';      // Load the cities straight from the server, passing the country as an extra param     $("#city_id").autocomplete({         source: function(request, response) {             $.ajax({                 url: src,                 dataType: "json",                 data: {                     term : request.term,                     country_id : $("#country_id").val()                 },                 success: function(data) {                     response(data);                 }             });         },         min_length: 3,         delay: 300     }); }); 
like image 168
ramonhimera Avatar answered Sep 23 '22 06:09

ramonhimera


Try this:

$('.ajax-auto input').setOptions({   extraParams: {     search_type: function(){       return $(this).attr('name');     }   } }) 

See also here

like image 34
Nam Le Avatar answered Sep 21 '22 06:09

Nam Le