Enter, press enter, and switch the input focus Method to be compatible with various browsers. Enter focus
During project creation, the customer requires that the input (Focus) can be directly switched using the enter key, and the information can be submitted directly at the last time.
The first idea is to copy a piece of code online and use it directly. However, Baidu and Google searched again and found the same code above 80%. Some codes are too old to be used. Some of them can only be used by some browsers. After half an hour, there is no proper solution. Finally, let's start it on your own.
I. Ideas
Each time you press enter, you can get the current focus position and set its next element to get the focus;
Ii. Code
<Script type = "text/javascript"> document. onkeydown = function enterHandler (event) {var inputs = $ ("input"); // you can add other filter conditions var browser = navigator. appName; // browser name var userAgent = navigator. userAgent; // obtain the browser's userAgent string var Code = ''; if (browser. indexOf ('internet')>-1) // IE Code = window. event. keyCode; else if (userAgent. indexOf ("Firefox")>-1) // Firefox Code = event. which; else // other Code = event. KeyCode? Event. keyCode: event. which? Event. which: event. charCode; if (Code = 13) // you can add other filter conditions {for (var I = 0; I <inputs. length; I ++) {if (inputs [I]. id = document. activeElement. id) {I = (inputs. length-1 )? -1: I; $ ('#' + inputs [I + 1]. id). focus () break ;}}</script>
Because IE and Firefox have different key values, they make a simple judgment on the browser. In this way, you can obtain the hitting key value in each browser.
Finally, after obtaining the current value, you can add other conditions.
How do I use the Enter key to switch the focus?
Use event to get the current action event object
Onkeypress event for input
If event. keycode = 13 then
Dim nextobj = document. getElementById ("nextobj ")
'Nextobj is the next input tag that requires focus
Nextobj. focus ()
End if
C # How to Implement the switch focus by pressing enter
Judge whether the keyboard key value is 13. If it is 13, the focus is switched to the next one.