简体   繁体   中英

Enter button issue in Firefox

I have a text field which will accepts only numbers. When the user types any characters and moves out of the textfield , using onchange I am checking whether user have entered Number or characters. So when user press tab , using onchange the value is checked. When the user press Enter button, it is set as window.event.keycode =9; as IE supports this. To make it work in other browsers, I have written logic to move the focus whenever the user presses the enter button.

The problem which I am facing is in Firefox, when the user presses enter button in the text field, now onchange is called as well as onsubmit is also called, which makes my page to refresh again. The logic which I have written to move the focus to next item , is also working. But I don't know why, onchange and onsubmit is called. This project composes of huge amount of code, thats why I am not able to post a piece of code. Any idea why it is working like this?

Some browsers have a global event object, other send the event object to the event handler as a parameter. Chrome and Internet Exlporer uses the former, Firefox uses the latter.

Some browsers use keyCode, others use charCode.

Enter key code is 13

    function Numberonly() {

                var reg = /^((\d{1,8}(\.\d{0,5})?%)|(\d{1,8}(\.\d{0,5})?))$/;

                if (!reg.test(($("#txtunitId").val()))) {
                    $("#txtunitId").val('');
                    return false;
                }
            }


 <input type="text" id="txtunitId" style="float: left;" onkeyup="Numberonly();" />

jsfiddle.net/hQ86t/20

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM