这几乎是这个问题的逐字帖.但是,一旦你转到jQuery 1.10,代码就不再有用了.
我确实将调用更改为"实时"方法,并将其切换为使用较新的"on"方法.我还需要做些什么来修复这个http://jsfiddle.net/sacredfaith/6t74T/458/?
$(function() {
var options = {
source: [
"ActionScript",
"AppleScript",
"Asp",
"BASIC",
"C",
"C++",
"Clojure",
"COBOL",
"ColdFusion",
"Erlang",
"Fortran",
"Groovy",
"Haskell",
"Java",
"JavaScript",
"Lisp",
"Perl",
"PHP",
"Python",
"Ruby",
"Scala",
"Scheme"
],
minLength: 2
};
$(document.body).on("keydown.autocomplete", "input.searchInput",function(){
$(this).autocomplete(options);
});
var addInput = function() {
var inputHTML = " <input name='search' value='' class='searchInput' maxlength='20' />";
$(inputHTML).appendTo("form#myForm");
$("input.searchInput:last").focus();
};
if (!$("form#myForm").find("input.searchInput").length) {
addInput();
}
$("input#addButton").click(addInput);
});
Run Code Online (Sandbox Code Playgroud)
到目前为止,我的搜索已经产生了使用弃用的jquery库的方法.我需要一个更新的版本,但在过去几个小时的工作中未能找到成功......
编辑:小提琴链接和代码更新,以反映"on"语法更改.
EDIT2:感谢您的耐心等待.
参考:对于1.10解决方案,请参阅所选答案的评论中的小提琴,否则使用1.9,直接在所选答案中查看小提琴.
我的要求是当用户在输入字段之一输入一些字符(至少 3 个)时显示几个选项,这些字符也可能会动态添加。
由于数据很大,我无法在页面加载时加载数据。有一个 ajax 调用来获取过滤后的数据。
我遇到的问题是Expected identifier第 2 行页面加载时出错。那么,您能告诉我下面的代码有什么问题吗?
$(document).on('keydown.autocomplete', 'input.searchInput', function() {
source: function (request, response) { // Line # 2
var id = this.element[0].id;
var val = $("#"+id).val();
$.ajax({
type : 'Get',
url: 'getNames.html?name=' + val,
success: function(data) {
var id = $(this).attr('id');
$(this).removeClass('ui-autocomplete-loading');
response(data);
},error: function(data) {
$('#'+id).removeClass('ui-autocomplete-loading');
}
});
},
minLength: 3
});
Run Code Online (Sandbox Code Playgroud)