2011-05-25 143 views
2

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"]}); 

所以问题是不是该功能缺失或图书馆没有加载或类似的东西。

而现在的问题

为什么?

回答

0

您使用自动填充小部件的方式过于复杂 - 小部件实际上是为了简化您的操作。你并不需要:

  1. 调用上KEYUP这个小部件/ click事件
  2. 设置超时
  3. 使AJAX调用

这里是你如何使用它:

$("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); 
    } 
    }); 
}); 

如果您去掉注释和开关盒逻辑,剩下的代码大概是5-6行。

+0

谢谢你的解释:) 我只需要为那些需要它的人添加一个require-autocompletion类。十分优雅。 – 2011-05-27 12:44:29

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}); 

您未设置源。

+0

@ user763228:从我看到的例子,作为参数传递的数组应该工作。 我试过,没有源选项。我从字面上复制了你的例子并再次尝试。 没有骰子。 – 2011-05-25 11:12:25

+1

如果您在msg var try,$(“#”+ fieldName).autocomplete({source:msg.split(',')})中创建了一个字符串而不是数组, – John 2011-05-25 11:15:51

+0

好的。我已经修改了PHP脚本来返回一个命令分隔字符串,并添加分裂到味精。 错误仍然存​​在: 响应: “蒙迪艾尔救援集团,蒙迪艾尔亚太地区,蒙迪艾尔救援集团,蒙迪艾尔毛里求斯,蒙迪艾尔泰国” 错误: $(“#” +字段名).autocomplete不是一个函数 行453 – 2011-05-25 11:23:51