我将此示例与 jQuery 1.9.1 一起使用
How to delay the .keyup() handler until the user stops typing?
在用户停止键入后延迟键盘请求。
// Custom Delay Function
var delay = (function(){
var timer = 0;
return function(callback, ms){
clearTimeout (timer);
timer = setTimeout(callback, ms);
};
})();
// Match Old Password
$('input[name="old_password"]').keyup(function(){
delay(function(){
var data = $.trim($(this).val());
// Send request to check
/*$.post('admin/ajax/passReq.php', {action: 'old_match', data: data}, function(response){
console.log('working');
});*/
console.log('working');
}, 2000 );
});
但我在 jquery 中收到 typeError: o.nodeName is undefined
:(
这在 1.9.1 上不起作用还是我必须以其他方式使用它?
最佳答案
您在延迟调用中使用this
。并且 $(this)
不会是文本框。
将其移到延迟函数调用之外:
$('input[name="old_password"]').keyup(function(){
var el = $(this);
// ^^^^^^^^^^^^^
delay(function(){
...
}, 2000 );
});
关于javascript - jquery keyup() 延迟不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17873309/