jQuery - 在用户键入或粘贴时更改文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24190527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery - on text change while user typing or paste
提问by Max Zag
I'm new to jQuery and I've tried to make something but I failed so here is my problem when the user type it works but when the user paste something didn't work !!
我是 jQuery 的新手,我试图做一些事情,但我失败了,所以当用户输入它时,这是我的问题,但当用户粘贴某些东西不起作用时!
$(document).ready(function(){
$('#username').keyup(username_check);
});
the username_check function :
username_check 函数:
function username_check(){
var username = $('#username').val();
if(username == "" || username.length < 4){
alert("error");
}
the field :
场 :
<input type="text" id="username" name="username" required>
回答by Shaunak D
Use .on()
or .bind()
to bind multiple events,
使用.on()
或.bind()
绑定多个事件,
$(document).ready(function(){
$('#username').on('keyup paste',username_check);
});
function username_check(){
setTimeout( function() {
var username = $('#username').val();
},100);
if(username == "" || username.length < 4){
alert("error");
}
}
回答by tymeJV
If you want to capture all input
events:
如果要捕获所有input
事件:
$('#username').on("input", username_check);