我特指 Jörn Zaefferer 的 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');
}
}
})
这是我的 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”字段不起作用,但您只需将值附加到请求查询字符串中即可。
$(document).ready(function() {
src = 'http://example.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 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2727859/