jQuery Autocomplete 使用 extraParams 传递额外的 GET 变量 [英] jQuery Autocomplete using extraParams to pass additional GET variables

查看:18
本文介绍了jQuery Autocomplete 使用 extraParams 传递额外的 GET 变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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.

我的 jQuery:

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

这是我的 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>

有什么建议吗?

推荐答案

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

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
    });
});

这篇关于jQuery Autocomplete 使用 extraParams 传递额外的 GET 变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆