【问题标题】:jQuery autocomplete - get the id of the autocomplete input fieldjQuery 自动完成 - 获取自动完成输入字段的 id
【发布时间】:2011-02-25 18:25:51
【问题描述】:

我有这个 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);
});

因为我将有几个带有自动完成字段的 ajax 加载表单, 我需要他们每个人发送适当的附加参数 从相应的表单到服务器。

我试图通过获取输入字段的 id 并使用它来获取将保存必要参数的国家字段的 id 来实现这一点。 id 将采用适当的格式以允许这样做。比如:

uni_1,国家/地区_1; uni_2; country_2

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

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

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

提前非常感谢您。 伊万

【问题讨论】:

  • 您使用的是哪个自动完成插件(它看起来不像 jQuery UI)?

标签: jquery jquery-plugins jquery-autocomplete


【解决方案1】:

你可以试试

$(".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);
    });
});

【讨论】:

    猜你喜欢
    • 2012-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-08
    • 2015-03-28
    相关资源
    最近更新 更多