Javascript uses the Enter key to implement the Tab key function

Source: Internet
Author: User

First, let's look at the effect of the following example:
<Input type = "text" NAME = ""> <br>
<Input type = "text" NAME = ""> <br>
<Input type = "text" NAME = ""> <br>
<Input type = "text" NAME = ""> <br>
<Input type = "text" NAME = ""> <br>
<Input type = "text" NAME = ""> <br>
<Input type = "text" NAME = ""> <br>
<Input type = "text" NAME = ""> <br>
<Script language = "javascript" for = "document" event = "onkeydown">
<! --
If (event. keyCode = 13)
Event. keyCode = 9;
-->
</Script>

Principle:
How to move the focus of the text box one by one with the "Enter key". Three methods are provided. The best one is as follows:

Use a client script to add the onkeydown event of the document to the page. After receiving the carriage return event, the page performs the Tab key function. That is, you only need to change the keyCode of the event from 13 to 9.

VBScript code:
<Script language = "vbscript">
Sub document_onkeydown
If event. keyCode = 13 then
Event. keyCode = 9
End if
End sub
</Script>
The Javascript code is as follows:
<Script language = "javascript" for = "document" event = "onkeydown">
<! --
If (event. keyCode = 13)
Event. keyCode = 9;
-->
</Script>
In this way, the focus can be moved down, but the buttons also play the same role. Generally, after entering the information, the customer jumps to the button, it is best to directly press "enter" to submit data. therefore, do not focus on the "Submit" button when modifying the preceding method. and directly activate the submission.

Therefore, I modified the preceding code to determine whether the event "Source" is a submit button. The Code is as follows:
<Script language = "javascript" for = "document" event = "onkeydown">
<! --
If (event. keyCode = 13 & event. srcElement. type! = 'Button '& event. srcElement. type! = 'Submit '& event. srcElement. type! = 'Reset' & event. srcElement. type! = 'Textea '& event. srcElement. type! = '')
Event. keyCode = 9;
-->
</Script>
Determines whether it is a button because there is type = "button" in HTML"
Determines whether it is a submit because there is type = "submit" in HTML"
Determines whether it is a reset because the "reset" on HTML should be executed.
Whether it is null is determined because the "<a> link" on HTML should also be executed. This situation does not happen much, you can use "tabindex =-1" to cancel the link and obtain the focus.

Related Article

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.