我想在停止在输入文本框中键入(而不是在键入时)字符之后立即触发事件.
I've tried with:
$('input#username').keypress(function() {
var _this = $(this); // copy of this object for further usage
setTimeout(function() {
$.post('/ajax/fetch', {
type: 'username',
value: _this.val()
}, function(data) {
if(!data.success) {
// continue working
} else {
// throw an error
}
}, 'json');
}, 3000);
});
但是这个示例 for each 键入的字符生成一个超时,如果我键入20个字符,我会收到大约20个Ajax请求.
On this fiddle我用一个简单的alert 而不是AJAX来演示同样的问题.
Is there a solution for this or I'm just using a bad approach for this?