以编程方式更改文本框值时 IE8 触发更改事件
我遇到了一些 IE8 javascript 问题(高兴)。
基本上,我需要将某个文本框中的任何负数条目转换为正值。我想我会这样做:
$('input[type="text"]').live('change', function () {
var number = $(this).val();
if (number < 0)
{
$(this).val(-number);
}
});
稍后,当文本框的焦点丢失时,我会做一些事情:
$('input[type="text"]').live('blur', function () {
// do stuff with textbox's _positive_ number
})
不幸的是,对于 IE8 当文本框的值更改时(即,您在文本框中输入 -23) ,更改事件被触发两次,而模糊事件根本没有被触发。
其他现代浏览器不这样做。
- 为什么更改事件被触发两次?
- 为什么模糊事件没有触发?
- 我该如何修复它?
转到此处查看发生的情况:http://jsfiddle.net/ajbeaven/AwZKM/
I'm having some IE8 javascript issues (joy).
Basically, I'm needing to convert any negative number entries in a certain textbox to a positive value. I figure I would do it like this:
$('input[type="text"]').live('change', function () {
var number = $(this).val();
if (number < 0)
{
$(this).val(-number);
}
});
Later on I do stuff when the textbox's focus is lost:
$('input[type="text"]').live('blur', function () {
// do stuff with textbox's _positive_ number
})
Unfortunately with IE8 when the value of the textbox is changed (ie. you enter -23 in the textbox), the change event is fired twice and the blur event is not fired at all.
Other modern browsers don't do this.
- Why is the change event fired twice?
- Why is the blur event not firing?
- How do I fix it?
Go here to see this happening: http://jsfiddle.net/ajbeaven/AwZKM/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您将表单元素的
value
属性设置为不同的值时,IE8 中会触发change
事件,因此此行会再次触发 Change 事件。如果我将代码更改为“它有效”,则模糊事件未触发的另一个问题
使我认为当更改和模糊事件与
.live
绑定时发生了一些事情。抱歉,我知道的解决方案不多,也许有人在读完本文后可以提出一些想法。When you set the
value
property of a form element to a different value, thechange
event fires in IE8 so this linetriggers the change event again. The other problem you are having with the blur event not firing, if i change the code to
It works, which makes me think something is going on when the change and blur event's are bound with
.live
. Sorry i know not much of a solution, maybe someone can come up with some ideas after reading this.