我有一个看起来像这样的函数:
<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"]});
所以问题不在于缺少函数或者没有加载库或类似的东西。
现在问题
为什么?!
答案 0 :(得分:1)
<script src="http://code.jquery.com/jquery-1.5.2.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.13/jquery-ui.min.js"></script>
$( "#"+fieldName ).autocomplete({source: msg} );
您没有设置来源。
答案 1 :(得分:0)
您使用自动完成小部件的方式过于复杂 - 小部件实际上应该为您简化操作。你不需要:
以下是您使用它的方式:
$("input.requires-autocomplete").each(function () { // add "requires-autocomplete" class to appropriate inputs
var action;
switch ($(this).attr("id").split("-")[1]) {
case "CompanyName":
action = "cv-company";
break; // you need to add breaks
case "DegreeName":
action = "degree-name";
break;
case "InstituteName":
action = "institute-name";
break;
case "LanguageName":
action = "language-name";
break;
case "CertificationName":
action = "certification-name";
break;
case "SkillName":
action = "skill-name";
break;
case "JobTitle":
action = "job-title";
break;
}
$(this).autocomplete({
minLength: 2, // widget waits until 2 or more characters are entered
delay: 500, // widget waits until 500 ms past the last keystroke
source: function (request, response) { // specifying a URL that returns JSON/JSONP was enough
// but in that case the server-side script must expect a query string parameter
// "term" (text inside the control) and "callback" (for JSONP requests)
// not your case so we do it manually
// we call your script via getJSON
// pass the text inside the control in the format expected by your script
// and call the response funciton passing in the JSON data
// the last statement is short-circuited by passing response as the second
// parameter to getJSON, which effectively calls the response function
// and passes in the response JSON data
$.getJSON("http://www.expat-job.com/ajax/" + action + "/keyword/" + request.term, response);
}
});
});
如果删除注释和switch-case逻辑,剩下的代码大约是5-6行。