在IE和FireFox中,Javascript onchange不同 [英] Javascript onchange different in IE and FireFox

查看:106
本文介绍了在IE和FireFox中,Javascript onchange不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当IE中的此onchange事件返回false时,IE焦点停留在该输入框上。在Firefox中,焦点总是移动到下一个字段。

When this onchange event in IE returns false, IE focus stays on that input box. In Firefox the focus always moves to the next field regardless.

HTML:

input name="seminar_donation" type="text" id="seminar_donation"
onchange="return CheckTotal(this);"

JavaScript:

JavaScript:

function CheckTotal(inputbox) {  
    if (isNaN(parseInt(inputbox.value))) {  
         alert("Please enter only digits 0-9");  
         inputbox.focus();  
         return false;  
    }  
    return true;  
}  

在IE中我甚至不需要inputbox.focus()似乎没有在Firefox中做任何事情来保持对错误输入框的关注。如何让Firefox保留在该输入框中?

In IE I don't even need the inputbox.focus() which unfortunately does not appear to do anything in Firefox to retain the focus on the errant input box. How can I get Firefox to stay on that input box?

推荐答案

此处不再出现的响应显示时间问题,虽然主题略有不同。所以我用谷歌搜索'超时'并发现2005年Mike Rankin的博客让我通过改变焦点()来解决问题:

A response that no longer appears to be here suggested 'timing' issues, albeit on a slightly different subject. So I googled 'timeout' and found Mike Rankin's blog from 2005 which allowed me to solve the issue by changing focus() to:

var t= setTimeout('document.getElementById("seminar_donation").focus()',1);

所以会发生什么是Firefox仍然会进入下一个字段,但是1毫秒后,此代码集重点回到错误的领域。这很麻烦,因为如果下一个字段有一个oblur事件,当超时强制焦点重新启动时会触发onb​​lur。但这是一个解决方案,显然是Firefox中长期存在的错误。

So what happens is Firefox still goes on to the next field, but 1 msec later, this code sets the focus back to the errant field. It is cludgy because if that next field has an oblur event that onblur will get triggered when the timeout forces the focus back. But it is a work-around for apparently a long standing bug in Firefox.

这篇关于在IE和FireFox中,Javascript onchange不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆