Jquery directly submits the form operation code in response to the Enter key.

Source: Internet
Author: User

Jquery directly submits the form operation code in response to the Enter key.

This is the case. On the login page, the Form is taken and Jquery Ajax is directly used to interact with the server. However, in this way, the browser will not respond to the carriage return key by default to submit data. Simply let Jquery take over the response of the Return key:
Copy codeThe Code is as follows:
$ ("# Loginbox input [type = 'submit ']"). click (function (){
// Verify the interaction between Ajax and the server
});
$ ('# Loginbox'). keydown (function (e ){
If (e. keyCode = 13 ){
// Simulate clicking the login button to trigger the above Click Event
$ ("# Loginbox input [type = 'submit ']"). click ();
}
});

If keydown is used, IE6 may be invalid. For the solution, proceed to the next step.

Keyboard Events include 3:

Keydown, keypress, keyup, respectively, are pressed, do not hold up, lift the keyboard.

The correct code is:
Copy codeThe Code is as follows:
$ (Document). keyup (function (event ){
If (event. keyCode = 13 ){
$ ("# Submit"). trigger ("click ");
}
});
Recommendation: keyup to prevent accidental touch of the notebook keyboard

1. Some documents are written as follows:
Copy codeThe Code is as follows:
$ (Window). keydown (function (){
...
})
XP IE6 is unsuccessful.

2. There are also INPUT
Copy codeThe Code is as follows:
$ ("Input"). keydown (function (){
...
})
In this case, the keyboard event can be monitored only when the input gets the focus.


HTML, how to press enter to submit a form

Change the type of the button to submit.
<Input type = submit value = "submit"/>
However, if you want to submit the event using jquery, stop the default event (e. stop () in the function that responds to the Action event and submit it using ajax.

In jquery, press enter to submit the form.

You can bind the form submission event directly without binding the Click Event of the submit button. jQuery provides the blocking method for form submission events: $ ("form"). submit. The above button binding event can be changed to binding the form submission event:
$ ('# MyForm '). submit (function () {var isOk = true ;//... here is the content in your click event if (when verification cannot be out of date) {isOk = false ;//... here, an error prompt is displayed.} // If the returned result is true, the request will be submitted. If the returned result is false, the return isOk;} is not submitted. The following is a complete example:
<Html>

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.