Javascript uses the carriage return key to replace the cursor movement function of the tab key.

Source: Internet
Author: User

Javascript uses the carriage return key to replace the cursor movement function of the tab key.

Blog type:

  • Javascript
Javascript

Add the following code to the page to be changed:

JS Code
  1. <SCRIPT type = "text/JavaScript">
  2. Document. onkeydown =FunctionEntertotab (){
  3. If(Event. srcelement. type! = 'Submit '& event. srcelement. type! = "Image" & event. srcelement. type! = 'Textea '& event. keycode = 13)
  4. Event. keycode = 9;
  5. }
  6. </SCRIPT>
<script type="text/javascript"> document.onkeydown=function enterToTab() { if(event.srcElement.type != 'submit' && event.srcElement.type!="image" && event.srcElement.type != 'textarea' && event.keyCode == 13) event.keyCode = 9; } </script>

To change the order in which the cursor moves, change the tabindex attribute of the form element.

HTML code
  1. <FormAction = "<% = PATH %>/ih99" name = "registerform">
  2. <InputType = "text" name = "username" tabindex = "1"/>
  3. <InputType = "text" name = "password" tabindex = "3"/>
  4. <InputType = "text" name = "checkcode" tabindex = "2"/>
  5. </Form>

Method 1: From tingtang. net

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! = Textarea & event. srcelement. type! =)

Event. keycode = 9;

-->

</SCRIPT>

Note: This method is really good, and the test is successful.

Method 2:

Here we will focus on the three objects of document, form, and elements and the onkeypress event, document
Document Object: a document that describes the current window or specified window object. It contains the content of the document from <Window Object>. Document (specify window); form object: Document. Forms [] is an array that contains all forms in the document
; Elements object: it is a form Element Object, <form Object>. Elements
Is an array that contains all objects of the form. The onkeypress event is a keyboard event that is triggered when you press the key on the keyboard. By combining these three objects with the onkeypress event, we can fulfill our requirements;

The following is the final implementation code list:
VaR jumptypearray = new
Array ("text", "password", "textarea", "checkbox", "radio ",

"Select", "select-one", "select-multiple", "file") // obtain the type of the Focus element on the reference page.
VaR
Debartypearray = new array ("Submit", "reset", "button", "checkbox", "radio ",

"Select", "select-one", "select-multiple", "hidden") // exclude element types on the reference page.
Function
Histogram array (SRC, DEC)
{// Check whether the specified string is in the specified string array. For example, if the index number is returned, no-1 is returned;
For (VAR I = 0;
I <dec. length; I ++)
{
If (src = dec [I])
{
Return I; // if the location is successful, the index number is returned.

Break;
}
}
Return-1; // An error occurred while locating.-1 is returned.
}
Function
Checkcr (EVT)
{// Return the Response Function
VaR EVT = (EVT )? EVT: (event )? Event: NULL );

VaR node = (evt.tar get )? Evt.tar get: (EVT. srcelement )? EVT. srcelement:
Null );
VaR FRM = Document. Forms [0]; // specify the form name as the first one on the page.
If (EVT. keycode =
13) & (using array (node. type, jumptypearray )! =-1 ))

{// Determines whether the current object is an input object. array of object types: jumptypearray
For (VAR I = 0; I <
FRM. elements. length; I ++)
{// For begin
If
(FRM. elements [I]. Name = node. Name)
{// Locate the current object
If (I + 1) <
FRM. elements. length)
{// Object array index offside judgment
If
(Using array (FRM. elements [I + 1]. type, jumptypearray )! =-1)
{
// Determine whether the next object of the current object is an input component. array of object types: jumptypearray
FRM. elements [I + 1]. Focus (); // sets the focus.

If (partition array (FRM. elements [I + 1]. type, debartypearray) =-1)
{
// Filter unselectable text objects. array of object types: debartypearray
FRM. elements [I + 1]. Select (); // select the component text content

}
}
Return false; // the content of the form cannot be submitted.
Break;
}
Else
{// Submit the form content

Return true;
Break;
}
}
} // End
}
}

Document. onkeypress = checkcr;

Reference Description: Save the above Code as a file, such as webenter. JS, and then write the following on the page to implement carriage return:
<Script
Language = "JavaScript" src = "path + webenter. js"> </SCRIPT>.

Path: the path of the webenter. js file.

Note: I have not tried this. Good luck.

 

Code for a page tested by IE8

<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<Head>
<Title> new document </title>
<Meta name = "generator" content = "editplus">
<Meta name = "author" content = "">
<Meta name = "keywords" content = "">
<Meta name = "Description" content = "">
<SCRIPT type = "text/JavaScript">
Document. onkeydown = function entertotab (){
If (event. srcelement. type! = 'Submit '& event. srcelement. type! = "Image" & event. srcelement. type! = 'Textea '& event. keycode = 13)
Event. keycode = 9;
}

Function INI () // when loading a page, the first option is selected
{
Document. getelementbyid ("A123"). Focus ();
}

Function checktext () // If the input in the pinyin header of the manufacturer is not empty, clear the Vendor Category ticket and select the ticket in front of the pinyin Letter of the manufacturer.
{
VaR textbox = Document. getelementbyid ("textbox1 ");
If (textbox. value! = "")
{
Document. Form. Leiyi. Checked = false;
Document. Form. leier. Checked = true;
}

}

Function twoselone1 () // when the first choice is selected, the second choice is canceled.
{
Document. Form. Leiyi. Checked = true;
Document. Form. leier. Checked = false;
}
Function twoselone2 () // when the second radio is selected, cancel the first radio
{
Document. Form. Leiyi. Checked = false;
Document. Form. leier. Checked = true;
}
</SCRIPT>
</Head>

<Body onload = "INI ()">
<Form name = "form">
<Input type = "radio" id = "A123" name = "Leiyi" tabindex = "1" Checked = "checked" onclick = "twoselone1 ()"/> vendor type
<Select tabindex = "2">
<Option> 1 </option>
<Option> 2 </option>
<Option> 3 </option>
<Option> 4 </option>
<Option> 5 </option>
</SELECT> <br/>
<Input type = "radio" id = "a456" name = "leier" tabindex = "3" onclick = "twoselone2 () "/> <input type =" text "id =" textbox1 "tabindex =" 4 "onblur =" checktext () "/>
<Input type = "Submit" value = "Submit" tabindex = "5" onclick = "javascript: Alert ('fuck');"/>

</Form>
</Body>
</Html>

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.