JavaScript uses the ENTER key to implement the TAB function

Source: Internet
Author: User
Tags end implement key reset
A lot of wordy customers like to put forward these needs! In fact, it is not difficult to achieve! The code is as follows:

I looked at MS's asp.net lectures some time ago, in which I talked about how to get the "enter" to move the focus of the text box down. One of the three methods, the best one is this:

Using client script to add the document's onkeydown event to the page, let the page perform the TAB function after receiving the carriage return event, that is, just change the event's KeyCode 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>

This way of handling, can realize the focus of moving down, but for the button also plays the same role, the general customer after entering the data, jump to the button, it is best to directly press "enter" to submit data. Therefore, the above method should be modified to the "submit" The button does not shift focus. Direct activation commits.

So I made a modification to the above code, that is, to determine the "source" of the event, is the 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!= ' textarea ' && event.srcelement.type! ='')

event.keycode=9;

-->

</script>

To determine if it is a button, because there will be a type= "button" on the HTML

To decide whether to submit, because the HTML will have type= "submit"

To determine if reset is because the "reset" on HTML should be executed

To determine whether it is empty, because the "link" on HTML should also be executed, this situation occurs not much, you can use the "Tabindex=-1" way to cancel the link to get 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.