Javascript jQuery 侦听所有文本输入更改

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

jQuery Listen For All Text Input Changes

javascriptjqueryhtmlformsevents

提问by Oliver Spryn

I have an input element within a form which I would like to be able to extract its value when the value changes. Take a look at this Fiddle: http://jsfiddle.net/spryno724/WaBzW/1/.

我在表单中有一个输入元素,我希望能够在值更改时提取其值。看看这个小提琴:http: //jsfiddle.net/spryno724/WaBzW/1/

By using the changeevent, I am able to display the value from the text input when the input looses focus. However, the value does not update when the form is reset or when JavaScript clears the value of the text input.

通过使用该change事件,我可以在输入失去焦点时显示来自文本输入的值。但是,当表单重置或 JavaScript 清除文本输入的值时,该值不会更新。

Is there a specific event that I can listen for which will be dispatched when anychange is made to a text input control?

是否有我可以侦听的特定事件,当对文本输入控件进行任何更改时将调度该事件?

I'd like to avoid work-arounds, such as listening for when the form is reset, or when the clear button is pressed. This is a simplified example of what my application does, and it will get crazy pretty fast if I try to do all of that.

我想避免变通办法,例如监听表单何时重置,或何时按下清除按钮。这是我的应用程序功能的简化示例,如果我尝试执行所有这些操作,它会很快变得疯狂。

Thank you for your time.

感谢您的时间。

采纳答案by Oliver Spryn

This question is an exact duplicate of another. See the answer with the highest number of up-votes as the answer to my question:

这个问题与另一个问题完全相同。将获得最高票数的答案作为我问题的答案:

JS Events: hooking on value change event on text inputs

JS 事件:在文本输入上挂钩值更改事件

回答by thecodeparadox

$(document).ready(function() {
    $('input.clear').click(function() {
        $('input.input').val('');
        $('p.display').text('The value of the text input is: ');
    });

    $('input.input').on('keyup change', function() {
       $('p.display').text('The value of the text input is: ' + $(this).val());
    });
})?

DEMO 1

演示 1

Probably this solution may help you:

可能这个解决方案可以帮助你:

$(document).ready(function() {
    $('input.clear, input[type=reset]').on('click', function() {
        $('input.input').val('').change();
        $('p.display').text('The value of the text input is: ');
    });

    $('input.input').on('keyup change', function() {
        $('p.display').text('The value of the text input is: ' + $(this).val());
    });
});?

DEMO 2

演示 2

回答by Raaghu

Override valmethod of jQuery

valjQuery的覆盖方法

HTML

HTML

<input id='myInput' name='myInputField' value='v1'>

<input id='myInput' name='myInputField' value='v1'>

JS

JS

var myInput = $('#myInput');

//this will not trigger the change event
myInput.val('v2');

//override val method
var oldVal = myInput.val;
myInput.val = function(value){
   var returnVal = oldVal.call(this,value);
   myInput.change();
   return returnVal;
}

// this will trigger the change event
// myInput.val is overridden
myInput.val('v3');

NOTEthis will work only if val method is called from myInputvariable

注意这只有在从myInput变量调用 val 方法时才有效

回答by Johnny Mnemonic

Try

尝试

$('#input').live('change',function() {         })