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 ';
}
}
});
});
});