jQuery自动完成 - 获取自动完成输入字段的ID

Iva*_*van 5 jquery jquery-plugins jquery-autocomplete

我有这个jQuery代码:

$(".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);
});
Run Code Online (Sandbox Code Playgroud)

因为我将有几个带有自动填充字段的ajax加载表单,我需要每个表单从相应的表单向服务器发送适当的附加参数.

我试图通过获取输入字段的id并使用它来获取将保存必要参数的country字段的id来实现此目的.id将采用适当的格式以允许此操作.就像是:

uni_1,country_1; uni_2; country_2

uni是自动完成字段,country是附加参数.

我已经意识到$(this).attr("id")将无法在自动完成中工作,就像使用简单的jQuery事件一样,我还没有找到这样做的方法.我尝试使用toSource方法来查看自动完成对象,但似乎它不包含字段ID.

所以,如果有人知道如何做到这一点,请分享.

非常感谢你提前.伊万

And*_*rea 6

你可以试试

$(".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);
    });
});
Run Code Online (Sandbox Code Playgroud)