当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));
}
})
}
});