如何将一个额外的隐藏按键传递到文本框?因为此代码需要一个额外的按键来加载自动完成下拉列表中的列表。
$( document ).ready(function() {
$("#zipcode").on("keyup", function(event) {
if(this.value.length == 5){
var zicpcode= $("#zipcode").val();
$.ajax({
url: "http://pages.em.essilorusa.com/page.aspx?QS=773ed3059447707d2a7242227e94bba8efcc7ce6da09facd&zip="+zicpcode,
type: "get", //send it through get method
success: function(results) {
var res=results.substring((results.indexOf("<rs1>")+5),results.indexOf("</rs1>"));
var splitted = res.split("|");
var distinct = [];
$.each(splitted , function(i, el){
if($.inArray(el, distinct ) === -1)
distinct.push(el);
});
$("#zipcode").autocomplete({ source: distinct });
},
});
}
});
});
答案 0 :(得分:1)
在.autocomplete()
已经有五个字符加载source
input type="text"
后,.autocomplete()
未初始化,直到第六个字符输入才会显示下拉列表?
尝试
$("#zipcode").autocomplete({
minLength:5,
source:function(request, response) {
var term = request.term;
if ( term in cache ) {
response( cache[ term ] );
return;
}
$.getJSON( "http://pages.em.essilorusa.com/page.aspx?QS=773ed3059447707d2a7242227e94bba8efcc7ce6da09facd&zip=" + term, function( results, status, xhr ) {
var res = results.substring((results.indexOf("<rs1>")+5),results.indexOf("</rs1>"));
var splitted = res.split("|");
var distinct = [];
$.each(splitted , function(i, el){
if($.inArray(el, distinct ) === -1)
distinct.push(el);
});
cache[ term ] = distinct;
response( distinct );
});
}
})
jsfiddle https://jsfiddle.net/g5zn9fs9/