检测 jQuery 中 input[type=text] 的值变化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8747439/
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 10:58:47  来源:igfitidea点击:

Detecting value change of input[type=text] in jQuery

jquery

提问by Jeriko

I want to execute a function every time the value of a specific input box changes. It almostworks with $('input').keyup(function), but nothing happens when pasting text into the box, for example. $input.change(function)only triggers when the input is blurred, so how would I immediately know whenever a text box has changed value?

每次特定输入框的值发生变化时,我都想执行一个函数。例如,它几乎适用于$('input').keyup(function),但在将文本粘贴到框中时没有任何反应。$input.change(function)仅在输入模糊时触发,那么我如何立即知道文本框的值发生了变化?

回答by Alejandro Silva

just remenber that 'on' is recomended over the 'bind' function, so always try to use a event listener like this:

请记住,在 'bind' 函数上推荐使用 'on',因此请始终尝试使用这样的事件侦听器:

$("#myTextBox").on("change paste keyup", function() {
   alert($(this).val()); 
});

回答by dknaack

Description

描述

You can do this using jQuery's .bind()method. Check out the jsFiddle.

您可以使用 jQuery 的.bind()方法来做到这一点。查看jsFiddle

Sample

样本

Html

html

<input id="myTextBox" type="text"/>

jQuery

jQuery

$("#myTextBox").bind("change paste keyup", function() {
   alert($(this).val()); 
});

More Information

更多信息

回答by andres

Try this.. credits to https://stackoverflow.com/users/1169519/teemu

试试这个.. 归功于https://stackoverflow.com/users/1169519/teemu

for answering my question here: https://stackoverflow.com/questions/24651811/jquery-keyup-doesnt-work-with-keycode-filtering?noredirect=1#comment38213480_24651811

在这里回答我的问题: https://stackoverflow.com/questions/24651811/jquery-keyup-doesnt-work-with-keycode-filtering?noredirect=1#comment38213480_24651811

This solution helped me to progress on my project.

这个解决方案帮助我推进了我的项目。

$("#your_textbox").on("input propertychange",function(){

   // Do your thing here.
});

Note: propertychange for lower versions of IE.

注意:低版本 IE 的属性更改。

回答by Ishan Jain

you can also use textbox events -

您还可以使用文本框事件 -

<input id="txt1" type="text" onchange="SetDefault($(this).val());" onkeyup="this.onchange();" onpaste="this.onchange();" oninput="this.onchange();">

function SetDefault(Text){
  alert(Text);
}

Try This

尝试这个

回答by H Bellamy

Try this:

尝试这个:

Basically, just account for each event:

基本上,只考虑每个事件:

Html:

网址:

<input id = "textbox" type = "text">

Jquery:

查询:

$("#textbox").keyup(function() { 
    alert($(this).val());  
}); 

$("#textbox").change(function() { 
alert($(this).val());  
}); 

回答by mohamad yazdanpanah

$("#myTextBox").on("change paste keyup select", function() {
     alert($(this).val());
});

select for browser suggestion

选择浏览器建议

回答by Hamed Khosravi

Try this:

尝试这个:

$("input").bind({
            paste : function(){
                $('#eventresult').text('paste behaviour detected!');
            }
})

回答by gilamran

Use this: https://github.com/gilamran/JQuery-Plugin-AnyChange

使用这个:https: //github.com/gilamran/JQuery-Plugin-AnyChange

It handles all the ways to change the input, including content menu (Using the mouse)

它处理改变输入的所有方式,包括内容菜单(使用鼠标)

回答by Nole

This combination of events worked for me:

这种事件组合对我有用:

$("#myTextBox").on("input paste", function() {
   alert($(this).val()); 
});