2013-02-11 119 views
0

我只是玩jQuery自动完成。我已经设置了最小长度为3.jquery自动完成不清除选择?

如果我输入字母“sci”,它会得到标题中带有字母“sci”的所有记录。这部分是正常工作,因为所有带有字母“sci”的记录都会返回并显示。

但是如果说我继续打字(当然是在暂停之后,此时我输入了“scisdfgdsfsd”),它仍然显示以前的选择。确实没有使用其中的字母“scisdfgdsfsd”的标题的记录。

有关如何解决此问题的任何想法?谢谢! :)行动的 “错误”


截图

工作:http://awesomescreenshot.com/0a2wuo2aa

不工作:http://awesomescreenshot.com/023wuo507

我jQuery代码

$(function() { 
    $("#course").autocomplete({ 
     minLength: 3, 
     source: function(request, response) { 
      $("#commentsSection").hide(); 
      $("#instanceIdSection").hide(); 
      $.getJSON("/issu/GetCourses.html", {term: request.term}, function(data, status) { 
       if (data.length > 0) { 
        response(data); 
       } else { 
        getEventComments(); 
        getEventSessions(); 
       } 
      }); 
     }, 
     select: function (event, ui) { 
      alert("select"); 
      getEventComments(); 
      getEventSessions(); 
     }, 
     change: function (event, ui) { 
      alert("change"); 
      getEventComments(); 
      getEventSessions(); 
     } 
    }); 

    function getEventSessions(){ 
     $.getJSON("/issu/GetEventSessions.html",{description: $("#course").val()}, function(data, status){ 
      if (data.length != 0) { 

       $("#instanceId").empty(); 
       $.each(data, function() { 
        $("#instanceId").append($('<option></option>').attr("value", $(this)[0]).text($(this)[1])); 
       }); 
       $("#instanceIdSection").show(); 
      } 
     }); 
    } 

    function getEventComments() { 
     $.get("/issu/GetEventComments.html",{description: $("#course").val()}, function(data, status){ 
      if (data.length != 0) { 
       $("#comments").text(data); 
       $("#commentsSection").show(); 
      } 
     }); 
    } 
}); 

回答

2

仅当有选项返回时,您提供给“源”选项的回调函数才会调用response()。如果没有选项返回,您还应该致电response()。这将导致自动完成程序清除结果。

尝试:

$.getJSON("/issu/GetCourses.html", {term: request.term}, function(data, status) { 
    response(data); 
    if (data.length == 0) { 
     getEventComments(); 
     getEventSessions(); 
    } 
}); 
+0

是使上周一些改变,完全错过了。谢谢! :) – mrjayviper 2013-02-11 02:08:09