根据我的经验,input type="text" onchange事件通常只发生在你离开(模糊)控件之后。

有办法迫使浏览器触发onchange每次文本字段内容的变化?如果不是,那么“手动”跟踪这种情况的最佳方法是什么?

使用onkey* events是不可靠的,因为您可以右键单击字段并选择粘贴,这将在没有任何键盘输入的情况下更改字段。

setTimeout是唯一的方法吗?丑:-)


当前回答

如果你只使用Internet Explorer,你可以使用这个:

<input type="text" id="myID" onpropertychange="TextChange(this)" />

<script type="text/javascript">
    function TextChange(tBox) {
        if(event.propertyName=='value') {
            //your code here
        }
    }
</script>

希望这能有所帮助。

其他回答

2018年到了,我是这么做的:

$(inputs).on('change keydown paste input propertychange click keyup blur',handler);

如果你能指出这种方法的缺陷,我会很感激。

您也可以使用按下键、按上键和按下键事件。

更新:

参见Another answer(2015)。


2009年原文答案:

你想让onchange事件触发按下键,模糊和粘贴?这是魔法。

如果你想在他们输入时跟踪更改,使用"onkeydown"。如果你需要用鼠标捕捉粘贴操作,请使用“onpaste”(IE, FF3)和“oninput”(FF, Opera, Chrome, Safari1)。

在Safari上<textarea>破碎。使用textInput代替

请,判断下一个方法使用JQuery:

HTML:

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

Javascript (JQuery):

$("#inputId").keyup(function() {
    $(this).blur();
    $(this).focus();
});

$("#inputId").change(function() {
    // Do whatever you need to do on actual change of the value of the input field
});

“输入”对我很有用。

var searchBar  = document.getElementById("searchBar");
searchBar.addEventListener("input", PredictSearch, false);