Sample Code for jquery to submit data by pressing enter _ jquery

Source: Internet
Author: User
Sample Code for jquery to submit data by press Enter. It is a simple method. You can see that it is very easy for jquery to submit data by press Enter, we only need to check whether the user presses the carriage return to bind the event directly. click to implement the submission. On the button, bind the ajax submit form event.
Core code

The Code is as follows:


$ (Document). ready (function (){
$ ("Press ENTER control"). keydown (function (e ){
Var curKey = e. which;
If (curKey = 13 ){
$ ("# Press ENTER event button control"). click ();
Return false;
}
});
});


The ajax function of js supports both carriage return events.

The Code is as follows:


Document. onkeydown = function (e ){
Var theEvent = window. event | e;
Var code = theEvent. keyCode | theEvent. which;
If (code = 13 ){
$ ("# Login_submit"). click ();
}
}

$ (Document). ready (function (){
// Login submission
$ ("# Login_submit"). bind ('click', function (){
Var username = $ ("# username"). val ();
Var password = $ ("# password"). val ();

$. Ajax ({
Type: 'post ',
Url: './login. php ',
Data: {type: 'ajax ', username: username, password: password },
Success: function (msg ){
// Alert (msg );
If (msg =-1 ){
Alert ('user name cannot be blank ');
$ ("# Username"). focus ();
}
If (msg =-2 ){
Alert ('user name does not exist ');
$ ("# Username"). val ("");
$ ("# Username"). focus ();
}
If (msg =-3 ){
Alert ('password cannot be blank ');
$ ("# Password"). focus ();
}
If (msg =-6 ){
Alert ('incorrect password key ');
$ ("# Password"). focus ();
}
If (msg = 1 ){
// Alert ('logon successful ');
Window. location. href = './index. php ';
}

}
});
});

});

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.