jquery 自动完成错误

发布于 2024-11-09 18:31:47 字数 3103 浏览 3 评论 0原文

我得到一个如下所示的函数:

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

海之角 2024-11-16 18:31:47
<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} ); 

您没有设置源。

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

You are not setting the source.

榕城若虚 2024-11-16 18:31:47

您使用自动完成小部件的方式过于复杂——该小部件实际上应该为您简化事情。您不需要:

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

如果去掉注释和 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:

  1. Invoke this widget on keyup/click events
  2. Set timeouts
  3. Make AJAX calls

Here is how you use it:

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

If you strip out the comments and the switch-case logic, the remaining code is roughly 5-6 lines.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文