4

ajax リクエストがトリガーされたときに typeahead 入力のクラスを変更し、リクエストが完了したときにクラスを削除するにはどうすればよいですか? 正しいイベントを使用しているかどうかわかりません。また、$(this) を使用して入力を参照することで、オブジェクト スコープが間違っていると思います。

$('.iof-subject').typeahead({
    source: function(typeahead, query) {
        $.ajax({
            url: window.app.ROOT + 'subjects/ajax/search_name',
            data: {name: query},

            // Add loading class to the text input (.iof-subject) when
            // the ajax request begins
            beforeSend: function() {
                $(this).addClass('loading');
            },

            // Remove loading class from text input (.iof-subject) when
            // the ajax request is complete
            complete: function() {
                $(this).removeClass('loading');
            },

            success: function(data) {
                return typeahead.process($.parseJSON(data));
            }
        })
    }
});
4

1 に答える 1