jquery implements a carriage return instead of the TAB key (press ENTER to skip to the next column)

Source: Internet
Author: User

After a submit button, if the user presses the keyboard key, the form will be submitted by default. This allows users to enter individual form items, the user experience is not good, enter an item, or use the mouse to select the next item, or use the Keyboard tab key to select the next item.

Second, the problem requires:

1: If the current focus (that is, the text box that the user is entering) is not the last input box, press ENTER to shift the focus to the next input box;

2: If the current focus (that is, the user is entering the text box) is the last input box, then press ENTER, the user will be asked to confirm and submit the form;

Three, the basic idea:

1: Determine whether the key is a carriage return to build, this is better to do, with the event in Jqeury, you can get to the current press the value of the key, the value of the Enter is 13, a comparison can be.

2: Determine whether the input box that is currently in focus is the last input box.

  1. $ (function () {
  2. $ ("form[name= ' contractform '] input:text"). KeyPress (function (e) {
  3. if (E.which = =) {//determine if press ENTER
  4. var inputs = $ ("form[name= ' Contractform ')"). Find (": Text"); //Get all the input boxes in the form
  5. var idx = Inputs.index (this); //Gets the position of the current focus input box
  6. if (idx = = inputs.length-1) {//Determine if it is the last input box
  7. if (Confirm ("The last input box has been entered, is it submitted?")) //user confirmation
  8. $ ("form[name= ' Contractform ')". Submit (); //Submit form
  9. } Else {
  10. Inputs[idx + 1].focus (); //Set focus
  11. Inputs[idx + 1].select (); //Select text
  12. }
  13. return false; To cancel the default commit behavior
  14. }
  15. });
  16. });

JQuery implements a carriage return instead of the TAB key (press ENTER to skip to the next column)

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.