English 中文(简体)
Javascript onchange different in IE and 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:

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

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?

问题回答
setTimeout( document.getElementById("seminar_donation").focus() ,1);

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);

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.

Have you tried adding a "return" in the onchange attribute? i.e.

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

I m not entirely sure that this would work, but worth a try?

Edit: to clarify, the reason I m not sure is that I d usually approach this differently and bind the event handler in javascript rather than html.





相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签