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.
- $ (function () {
- $ ("form[name= ' contractform '] input:text"). KeyPress (function (e) {
- if (E.which = =) {//determine if press ENTER
- var inputs = $ ("form[name= ' Contractform ')"). Find (": Text"); //Get all the input boxes in the form
- var idx = Inputs.index (this); //Gets the position of the current focus input box
- if (idx = = inputs.length-1) {//Determine if it is the last input box
- if (Confirm ("The last input box has been entered, is it submitted?")) //user confirmation
- $ ("form[name= ' Contractform ')". Submit (); //Submit form
- } Else {
- Inputs[idx + 1].focus (); //Set focus
- Inputs[idx + 1].select (); //Select text
- }
- return false; To cancel the default commit behavior
- }
- });
- });
JQuery implements a carriage return instead of the TAB key (press ENTER to skip to the next column)