jquery自动完成没有清除选择?

时间:2013-02-11 00:53:33

标签: jquery jquery-ui jquery-ui-autocomplete

我正在玩jquery自动完成功能。我已将最小长度设置为3。

如果我输入字母“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();
            }
        });
    }
});

1 个答案:

答案 0 :(得分:2)

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

尝试:

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