JQuery controls the carriage return to focus the controls in the form

Source: Internet
Author: User

<Script type = "text/javascript" src = "jquery-1.4.4.min.js"> </script>
<Script type = "text/javascript">

$ (Function (){
Var length = $ (": input"). length;
$ (": Input"). keyup (function (e ){
Var key = e. which;
If (13 = key ){
Var index = $ (": input"). index (this );
Var newIndex = index + 1;
If (length = newIndex)
{
NewIndex = 0;
}
$ (": Input: eq (" + newIndex + ")"). focus ();
}
});
});

</Script>
</Head>
<Body>
<Form id = "frm1">
<Input type = "text"/> <br/>
<Input type = "text"/> <br/>
<Select>
<Option> option 1 </option>
<Option> option 2 </option>
</Select>
<Br/>
<Input id = "btn" type = "button" value = "Submit"/>
</Form>
</Body>

 

Notes

① $ (": Input") indicates all controls in the form. Unlike $ ("input"), only the input tag is obtained, and select is not obtained.
② The index function is a useful function in jQuery.


However, in actual situations, we do not have to cyclically obtain the focus. When the submit button gets the focus, we submit the form.

$ (Function (){
$ (": Input"). keyup (function (e ){
Var key = e. which;
If (13 = key ){
Var index = $ (": input"). index (this );
Var newIndex = index + 1;
$ (": Input: eq (" + newIndex + ")"). focus ();
}
});

$ ("# Btn"). click (function (){
$ ("Frm1"). submit ();
});

});


 

From IT fat Column

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.