为什么jquery Autocomplete minlength属性不能与ajax调用一起使用

时间:2015-02-02 13:49:10

标签: jquery autocomplete

我正在使用Jquery UI Ajax自动完成文本字段。

这是我的代码

http://jsfiddle.net/41w8b23q/1/

<input type="text" name="state" id="state" placeholder="state" required="" onkeypress="return nospecialCharacters(event)" class="ui-autocomplete-input error" autocomplete="off">

$("#state").on("keydown", function(event) {
    var statevalue = $("#state").val();
    if (statevalue) {
      $.ajax({
        type: 'GET',
        url: url + '/HHH/JUI/chdautosuggestatemodified?state=' + statevalue,
        jsonpCallback: 'jsonCallback',
        cache: true,
        dataType: 'jsonp',
        jsonp: false,
        success: function(respons) {
          $("#state").autocomplete({
             noResults : '',
            source: respons,
            minLength: 1
          });
        },
        error: function(e) {}
      });
    } else {
      //prevents();
      event.stopPropagation();
      //      event.preventDefault();
      event.stopImmediatePropagation();
    }
  });

现在我面临两个问题。

问题1

当我在该文本字段上非常快速地输入数据时,我得到了    未捕获的TypeError:undefined不是浏览器控制台下的函数

第2期

我面临的另一个问题是,即使我提到minlength为1,它还没有显示数据,直到我输入3个字符?

问题1不是那么重要但问题2是我需要解决的问题,请你告诉我如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

您使用的自动填充错误。你不必照顾关键事件。自动完成插件为您完成。你必须告诉autocomplete consturctor你的source将是一个ajax调用。 看看这个帖子How to use source: function()... and AJAX in JQuery UI autocomplete

中的答案