jQuery自动完成帖子请求仅一次 [英] Jquery autocomplete post request only once
问题描述
我正在使用jQuery自动完成功能.我对下面的代码不满意,因为必须有更好的方法来执行此操作. (代码有效)
I am using jquery autocomplete. I am not happy with the code below because there must be a better way to do this. (code works)
- 我希望用户在搜索框中输入3个字母
- 自动完成发布参数和服务器响应
- 我们收到来自服务器的数据后,没有理由再次发布. 注意:每当用户添加新字母时,自动完成功能都会保留postind数据
- 因为我有数据,所以我想将此数据用作自动填充的来源
- I want the user to enter 3 letters into the search box
- autocomplete post parameters and server responds
- After we have the data coming from server there is no reason to post again. note: autocomplete will keep on postind data every time user adds a new letter
- since I have the data, I want to use this data as source of autocomplete
下面的代码有一个小问题.选择一个时,它将显示在框中.当我使用退格键时,它不会刷新/填充建议.
The code below works with a little problem. on select one it is display in the box. When I use backspace it doesnt refresh/populate the suggestions.
问题: 如何使用自动完成功能仅发布一次并重复使用响应和自动完成功能的来源?
Question: How can I use autocomplete to post only once and reuse the response adn the source for the autocomplete?
如果有人可以建议或指出我可以阅读的任何文件.
If can anyone suggest or point any document I could read.
谢谢
jquery-1.9.1.min.js
jquery-ui-1.10.0/jquery-ui.js
$("#birds").autocomplete
({
source: function(request, response) {
$.ajax(
{
url: "page.aspx",
data: {
Para: request.term,
GroupID: 11,
rGUI: 'd9',
rURL: 'domain.com/',
task: 'get Stuff'
},
type: "POST",
dataType: "json",
success: function(data) {
//response: function(item) {
var arr = $.map(data, function(item) {
return {
label: item.LN60,
value: item.LN60,
NDC: item.NDC
}
});
$("#birds").autocomplete({
source: arr,
minLength: 1
});
$("#birds").autocomplete({
close: function(event, ui) {
suggestDrugs_Request();
}
});
}
});
},
minLength: 2,
select: function(event, ui) {
log(ui.item ? "Selected: " + ui.item.value +
" aka " + ui.item.NDC : "Nothing selected, input was "
+ this.value);
}
});
推荐答案
除了将ajax调用添加到自动完成功能的source属性中,您应该做的是将自动完成功能添加到ajax调用的回调中:>
What you should do instead of adding the ajax call to the source property of the autocomplete, is add the autocomplete functioniality to the callback of the ajax call:
$.ajax({
//all your other call settings
success: function(data){
//init autocomplete
$('#birds').autocomplete({
source: data,
//all your other autocomplete settings
});
}
});
这篇关于jQuery自动完成帖子请求仅一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!