开发者

jQuery autocomplete - get the id of the autocomplete input field

开发者 https://www.devze.com 2023-02-14 00:35 出处:网络
i have this jQuery code: $(\".autocomplete\").autocomplete(\'url\',{ extraParams: {country: function(){

i have this jQuery code:

$(".autocomplete").autocomplete('url',{
  extraParams: {country: function(){
    var id = $(this).attr("id"); // not working
    var country = id.replace("uni", "country");
    var country_id = $("#"+country).val();
    return country_id;
  }}            
}).result(function(event, item) {
 // getNames(item);
});

As I will have several, ajax loaded forms with autocomplete fields, i need for each of them to send appropriate additional parameters from the respective form to the server.

I was trying to achieve this by getting the id of the input field and using it to get to the id of the country field which will hold the necessary parameter. The id's would be in the appropriate format to allow for this. Something like:

uni_1, country_1; uni_2; country_2

uni being the autocomplete field, and country the additional parameter.

I have realised that $(this).attr("id") will not work inside the autocomplete, as it would with simple jQuery events, and I have not found a way of doing this. I tried using the toSource method to see the autocomplete object, but it seems that it does not hold t开发者_JS百科he field id.

So if anyone has an idea how this could be done, please share.

Thank you very much in advance. Ivan


You can try

$(".autocomplete").each(function() {
  var id = $(this).attr("id");
  $(this).autocomplete('url',{
      extraParams: {country: function(){
        var country = id.replace("uni", "country");
        var country_id = $("#"+country).val();
        return country_id;
      }}            
    }).result(function(event, item) {
     // getNames(item);
    });
});
0

精彩评论

暂无评论...
验证码 换一张
取 消