jquery 自动完成错误
我得到一个如下所示的函数:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js" type="text/javascript"></script>
<script src="/js/jquery-ui-1.8.13.custom.min.js" type="text/javascript"></script>
function getValues (fieldName, action){
$("#" + fieldName).keyup(function () {
if (this.value != this.lastValue){
if (this.timer) clearTimeout(this.timer);
this.timer = setTimeout(function () {
//$( "#"+fieldName ).autocomplete({source:"http://www.expat-job.com/ajax/" + action + "/keyword/" + $("#" + fieldName).val()});
$.ajax({
type: "POST",
dataType: 'json',
url:"http://www.expat-job.com/ajax/" + action + "/keyword/" + $("#" + fieldName).val(),
success:function(msg) {
//splitedmsg = msg.split(',');
$( "#"+fieldName ).autocomplete(msg);
}
});
}, 200);
this.lastValue = this.value;
}
});
}
然后像这样调用它:
$('input').live('click', function() {
var word = $(this).attr('id');
var splitedWord = word.split('-');
switch(splitedWord[1])
{
case 'CompanyName':
getValues(word, 'cv-company');
case 'DegreeName':
getValues(word, 'degree-name');
case 'InstituteName':
getValues(word, 'institute-name');
case 'LanguageName':
getValues(word, 'language-name');
case 'CertificationName':
getValues(word, 'certification-name');
case 'SkillName':
getValues(word, 'skill-name');
case 'JobTitle':
getValues(word, 'job-title');
}
});
ajax 响应如下所示:
["Mondial Assistance","Mondial Assistance Asia Pacific","Mondial Assistance Group","Mondial Assistance Mauritius","Mondial Assistance Thailand"]
它是一个包装在 json_encode() 中的数组。
我的问题在于自动完成部分:
$( "#"+fieldName ).autocomplete(msg);
我已经尝试了所有可能的方法来输入数据。我已经回显了一个字符串并将其拆分以获得一个数组。
我使用了不同的语法: $( "#"+fieldName ).autocomplete({source: msg});
我总是收到相同的错误消息:
$("#" + fieldName).autocomplete is not a function
success()cv (line 453)
msg = "["Mondial Assistance","...l Assistance Thailand"]"
F()jquery.min.js (line 19)
F()jquery.min.js (line 19)
X = 0
经过大量测试,我发现它可以通过这样的简单测试工作:
$( "#"+fieldName ).autocomplete({source: ["orange","apple","pear"]});
所以问题不在于函数丢失或库未加载或类似的情况。
现在的问题
是为什么?!
I got a function that looks like this:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js" type="text/javascript"></script>
<script src="/js/jquery-ui-1.8.13.custom.min.js" type="text/javascript"></script>
function getValues (fieldName, action){
$("#" + fieldName).keyup(function () {
if (this.value != this.lastValue){
if (this.timer) clearTimeout(this.timer);
this.timer = setTimeout(function () {
//$( "#"+fieldName ).autocomplete({source:"http://www.expat-job.com/ajax/" + action + "/keyword/" + $("#" + fieldName).val()});
$.ajax({
type: "POST",
dataType: 'json',
url:"http://www.expat-job.com/ajax/" + action + "/keyword/" + $("#" + fieldName).val(),
success:function(msg) {
//splitedmsg = msg.split(',');
$( "#"+fieldName ).autocomplete(msg);
}
});
}, 200);
this.lastValue = this.value;
}
});
}
It is then called like this:
$('input').live('click', function() {
var word = $(this).attr('id');
var splitedWord = word.split('-');
switch(splitedWord[1])
{
case 'CompanyName':
getValues(word, 'cv-company');
case 'DegreeName':
getValues(word, 'degree-name');
case 'InstituteName':
getValues(word, 'institute-name');
case 'LanguageName':
getValues(word, 'language-name');
case 'CertificationName':
getValues(word, 'certification-name');
case 'SkillName':
getValues(word, 'skill-name');
case 'JobTitle':
getValues(word, 'job-title');
}
});
The ajax response looks like this:
["Mondial Assistance","Mondial Assistance Asia Pacific","Mondial Assistance Group","Mondial Assistance Mauritius","Mondial Assistance Thailand"]
It's an array wrapped in json_encode().
My problem lies in the autocomplete part:
$( "#"+fieldName ).autocomplete(msg);
I have tried every way possible to input the data. I've echoed a string and split it to get an array.
I've used different syntax:
$( "#"+fieldName ).autocomplete({source: msg});
I always get the same error message:
$("#" + fieldName).autocomplete is not a function
success()cv (line 453)
msg = "["Mondial Assistance","...l Assistance Thailand"]"
F()jquery.min.js (line 19)
F()jquery.min.js (line 19)
X = 0
After a lot of testing, I've found out that it works with a simple test like this:
$( "#"+fieldName ).autocomplete({source: ["orange","apple","pear"]});
So the problem is not that the function is missing or the library is not loaded or anything like that.
And now the question
Why?!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您没有设置源。
You are not setting the source.
您使用自动完成小部件的方式过于复杂——该小部件实际上应该为您简化事情。您不需要:
以下是如何使用它:
如果去掉注释和 switch-case 逻辑,剩余的代码大约为 5-6 行。
The way you're using the autocomplete widget is overly complicated -- the widget is actually supposed to simplify things for you. You DO NOT need to:
Here is how you use it:
If you strip out the comments and the switch-case logic, the remaining code is roughly 5-6 lines.