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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 02:29:25  来源:igfitidea点击:

jQuery - on text change while user typing or paste

jquerytextkeyup

提问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");
    }
}

Working Fiddle

工作小提琴

回答by tymeJV

If you want to capture all inputevents:

如果要捕获所有input事件:

$('#username').on("input", username_check);