Javascript Onchange, Onblur, And Focus Weirdness In Firefox
On my form I have a discount field that accepts a dollar amount to be taken off of the total bill (HTML generated in PHP): echo '
Solution 1:
You might want to try the technique described here: Javascript / Firefox / onBlur
I haven't tried it myself, but essentially it suggests to replace document.form.discount.focus()
with
setTimeout(function() {document.form.discount.focus();}, 1);
I suspect the underlying problem is that when you hit tab (for example), the browser goes through a few steps: call the code assosiated with the current control (onchange, onblur), then set the focus to the new control. So if you change focus in the first step, then the focus will still get reset immediately in the next step. Hence the timer-based workaround.
Solution 2:
You better convert the discount to a number when you are using it in number comparisions. Change your script to:
if(discount != ''){
discount = parseFloat(discount);
if(discount - 0.01 > total_bill){
window.alert('Discount Cannot Be Greater Than Total Bill');
document.form.discount.focus();
}
else{
total_bill -= discount;
}
}
Post a Comment for "Javascript Onchange, Onblur, And Focus Weirdness In Firefox"