根据我的经验,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>

希望这能有所帮助。

其他回答

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

如果你只使用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>

希望这能有所帮助。

使用oninput而不是onchange。

index . html

<html>
  <head>
    <title>title here</title>
    <script src="index.js"></script>
  </head>
  <body>
    <input oninput="onclickhandler(this)"></input>
  </body>
</html>

script.js

function onclickhandler(e) {
  console.log(e.value);
}

下面的代码为我工作良好的Jquery 1.8.3

HTML: <输入类型=“文本”id=“myId”/>

Javascript/JQuery:

$("#myId").on('change keydown paste input', function(){
      doSomething();
});

“输入”对我很有用。

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