简体   繁体   English

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

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

I want to execute a function every time the value of a specific input box changes.我想在每次特定输入框的值发生变化时执行 function。 It almost works with $('input').keyup(function) , but nothing happens when pasting text into the box, for example.几乎可以与$('input').keyup(function)一起使用,但是例如,将文本粘贴到框中时没有任何反应。 $input.change(function) only triggers when the input is blurred, so how would I immediately know whenever a text box has changed value? $input.change(function)仅在输入模糊时触发,那么当文本框更改值时我如何立即知道?

Update - 2021更新 - 2021

As of 2021 you can use input event for all the events catering input value changes.从 2021 年开始,您可以将input事件用于满足输入值更改的所有事件。

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

Original Answer原答案

just remember that 'on' is recommended 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()); 
});

Description描述

You can do this using jQuery's .bind() method.您可以使用 jQuery 的.bind()方法来做到这一点。 Check out the jsFiddle .查看jsFiddle

Sample样本

Html html

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

jQuery jQuery

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

More Information更多信息

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 的属性更改。

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尝试这个

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());  
}); 
$("#myTextBox").on("change paste keyup select", function() {
     alert($(this).val());
});

select for browser suggestion选择浏览器建议

DON'T FORGET THE cut or select EVENTS!不要忘记cutselect活动!

The accepted answer is almost perfect, but it forgets about the cut and select events.接受的答案几乎是完美的,但它忘记了cutselect事件。

cut is fired when the user cuts text (CTRL + X or via right click) cut在用户剪切文本时触发(CTRL + X 或通过右键单击)

select is fired when the user selects a browser-suggested option当用户选择浏览器建议的选项时触发select

You should add them too, as such:您也应该添加它们,例如:

$("#myTextBox").on("change paste keyup cut select", function() {
   //Do your function 
});

Try this:尝试这个:

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

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)它处理改变输入的所有方式,包括内容菜单(使用鼠标)

This combination of events worked for me:这种事件组合对我有用:

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

You can use the following code to detect input, keychange, and keypress.您可以使用以下代码来检测输入、按键更改和按键。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM