javascript Onchange 事件运行不正常

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

Onchange event is not working well

javascripthtmleventsonchange

提问by zanhtet

I created a following html page.

我创建了以下 html 页面。

<html>
    <head></head>
    <body>
        <input type="text" value="" id="Textbox" onchange="alert('text change');" />
        <input type="button" value="" onclick="document.getElementById('Textbox').value = 'Hello';" />
    </body>
</html>

When the entered text is inputted into the textbox, the onchange event is working well. I wrote a code that when the button is clicked, the 'Hello' text is inputted into the the textbox. But the onchange event of the textbox is not working well. How can I catch change event? Thanks.

当输入的文本输入到文本框时,onchange 事件运行良好。我写了一个代码,当点击按钮时,“你好”文本被输入到文本框中。但是文本框的 onchange 事件运行不正常。如何捕捉更改事件?谢谢。

回答by RobG

Programmatically changing the value doesn't fire a change event, that only occurs if the user focuses the element, changes the value and then puts focus elsewhere.

以编程方式更改值不会触发更改事件,仅当用户聚焦元素、更改值然后将焦点放在其他地方时才会发生。

Options are to manually call the onchange listener, dispatch a change eventon the element, or manually bubble the change event by going up the DOM looking for parents with an onchange listener and calling them.

选项是手动调用 onchange 侦听器,在元素上调度更改事件,或者通过在 DOM 上查找具有 onchange 侦听器的父项并调用它们来手动冒泡更改事件。

Here is an answer that seems to fit the bill: trigger onchange event manually

这是一个似乎符合要求的答案:手动触发 onchange 事件

Some links:

一些链接:

  1. MDN dispatchEvent (standards compliant): https://developer.mozilla.org/en/DOM/element.dispatchEvent
  2. MSDN fireEvent (IE proprietary): http://msdn.microsoft.com/en-us/library/ie/ms536423(v=vs.85).aspx
  1. MDN dispatchEvent(符合标准):https: //developer.mozilla.org/en/DOM/element.dispatchEvent
  2. MSDN fireEvent(IE 专有):http: //msdn.microsoft.com/en-us/library/ie/ms536423(v=vs.85) .aspx

回答by hobberwickey

If you want to capture every change use the keydown event. onChange only fire after an input is blurred if I remember correctly.

如果要捕获每个更改,请使用 keydown 事件。如果我没记错的话,onChange 只会在输入模糊后触发。

回答by xandercoded

function codeThatAltersTextFieldProgrammatically() {
    /* ... code ...  */

    textFieldChangeEventHandler();
};

function textFieldChangeEventHandler(){
    /* .... */
}

回答by mfadel

well currently according to your code, when you get out of the text box the event works,

目前根据您的代码,当您离开文本框时,事件有效,

you need to try OnKeyPress check it here http://jsfiddle.net/EBMyy/

你需要尝试 OnKeyPress 检查这里http://jsfiddle.net/EBMyy/

see this code

看到这个代码

$(document).ready(function(){
    $("#Textbox").keypress(function(){
        alert("Text Changed");
    });
});?