使用extraParams传递额外的GET变量的jQuery自动完成

pap*_*lip 28 jquery jquery-plugins autosuggest jquery-autocomplete

我指的是JörnZaefferer的jQuery Autocomplete v1.1插件[来源:http://bassistance.de/jquery-plugins/jquery-plugin-autocomplete/],因为这个插件似乎有很多变种.

我正在尝试在用户开始输入时将其他参数传递给服务器,因为我有多个字段,我希望自动完成提供建议.

除了查询之外,我想将输入名称属性发送到服务器,但我似乎无法在extraParams中使用$(this).attr('name').

我的jQuery:

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

这是我的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>
Run Code Online (Sandbox Code Playgroud)

有什么建议?

小智 47

我正在使用自动完成功能,现在是jquery ui的一部分.传递'extraParams'字段不起作用,但您可以在请求查询字符串中附加值.

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

  • 需要注意的是,对于当前建议的使用jQuery UI中内置的自动填充扩展器的方法,这是正确的答案.它让新用户感到困惑,因为有很多关于'JörnZaefferer'版本的博客和书面文章,它是内置版本的前身.http://www.learningjquery.com/2010/06/autocomplete-migration-guide (4认同)