如何检测文本输入何时随ie8变化

我想检测文本输入何时更改。 我试过这些,它们在firefox中工作但不在8中。

$('#taskSearch').bind('input', function() { alert($(this).val()); }); $('#taskSearch').live('input', function() { alert($(this).val()); }); $('#taskSearch').change(function() { alert($(this).val()); }); 

你可以使用onpropertychange for IE6 +:

 $("#taskSearch").on("propertychange", function(){ alert($(this).val()); }); 

最后一个(并且只有最后一个)是正确的,但是您缺少一个右括号:

 $('#taskSearch').change(function() { alert($(this).val()); }); 

.live()已弃用(且语法不正确) .bind()的语法也不正确; 事件的名称是'change' ,而不是'input' 。 请参阅.change()的文档。

以下解决方案适用于IE8和现代浏览器,用于按键,滚动或a type =“num”输入字段中的箭头按钮进行更改:

 $('#element').on('keyup change', function() { // do something }); 

https://github.com/spicyj/jquery-splendid-textchange是一个插件,用于修复在IE8和IE9中模拟“输入”的怪癖。

作者描述了他在博客文章中如何实现这一解决方案( http://benalpert.com/2013/06/18/a-near-perfect-oninput-shim-for-ie-8-and-9.html ),这确实很复杂,如果你想知道细节,请阅读。

 $('#taskSearch').change(function() { alert($(this).val()); // not the extra brace I've added }); 

应该只是工作。 PS 请停止使用.live()

这将检测IE8中按键的时间

$(“#input”)。on(’keyup’,function(event){alert(’keypress’);});

要在输入值更改时立即触发:

 $('#example')on('keyup input', function(e){ alert(e.type); // Displays 'keyup' in IE8, 'input' in browsers }); 

(这是Mark答案的变体)