我有一个带有 onchange 事件的文本框。为什么当用户使用自动完成功能填充文本框时不会触发此事件?
我正在使用 Internet Explorer。是否有标准且相对简单的解决方案来解决此问题,而无需禁用自动完成功能?
我有一个带有 onchange 事件的文本框。为什么当用户使用自动完成功能填充文本框时不会触发此事件?
我正在使用 Internet Explorer。是否有标准且相对简单的解决方案来解决此问题,而无需禁用自动完成功能?
上次我遇到这个问题时,我最终onpropertychange
改为使用Internet Explorer的事件。我在 MSDN 上读到了这个:这是绕过它的推荐方法。
之前遇到过这个烦人的功能,当时我的解决方法是使用onfocus事件记录文本框的当前值,然后使用onblur事件检查当前值是否与onfocus时保存的值相匹配。例如
<input type="text" name="txtTest" value="" onfocus="this.originalvalue=this.value" onblur="if (this.value != this.originalvalue) alert('Test has changed')"/>
基于 Tim C 的回答,这是我想出的简单 jquery 来处理页面上所有文本框的问题:
$("input[type=text]").bind("focus change",function(){
this.previousvalue = this.value;
}).blur(function(){
if (this.previousvalue != this.value){
$(this).change();
}
})
如果你不关心 onchange 多次触发,你可以让它更简单:
$("input[type=text]").blur(function(){
$(this).change();
})
我刚刚发现使用 jQuery 1.4 设置更改事件可以解决这个问题。如果您已经熟悉 jQuery,这似乎是解决此问题的最简单方法。
我只是为需要触发文本更改事件的文本框关闭自动完成功能
'Turn off Auto complete
'(it needs to fire the text change event)
txtYourTextBox.Attributes.Add("AutoComplete", "off")
只需将其放在页面加载中即可。
或者,您可以将属性添加到input
元素或整个 的标记中form
:
<input type=text autocomplete=off>
或者
<form autocomplete=off>