0

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

1 に答える 1

2

「ソース」オプションに提供するコールバック関数は、response()返されたオプションがある場合にのみ呼び出します。response()オプションが返されない場合にも呼び出す必要があります。これにより、オートコンプリーターが結果をクリアします。

試す:

$.getJSON("/issu/GetCourses.html", {term: request.term}, function(data, status) {
    response(data);
    if (data.length == 0) {
        getEventComments();
        getEventSessions();
    }
});
于 2013-02-11T01:36:46.367 に答える