Enter, press enter, and switch the input focus Method to be compatible with various browsers. Enter focus

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.