Jquery code sample to submit data by carriage return

  • 2020-03-26 23:02:01
  • OfStack

In fact, jquery press enter to submit data is a very simple thing, we just need to check that the user press enter to directly bind the event.
The core code
 

$(document).ready(function(){ 
$(" Press the enter control ").keydown(function(e){ 
var curKey = e.which; 
if(curKey == 13){ 
$("# Enter event button control ").click(); 
return false; 
} 
}); 
});
 

It is to support the carriage return event with the ajax function of js

document.onkeydown = function (e) { 
var theEvent = window.event || e; 
var code = theEvent.keyCode || theEvent.which; 
if (code == 13) { 
$("#login_submit").click(); 
} 
}  

$(document).ready(function() { 
    //Log in to submit
    $("#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(' The username cannot be empty '); 
                        $("#username").focus(); 
                    } 
                    if(msg==-2){ 
                        alert(' The username does not exist '); 
                        $("#username").val(""); 
                        $("#username").focus(); 
                    } 
                    if(msg==-3){ 
                        alert(' The password cannot be empty '); 
                        $("#password").focus(); 
                    } 
                    if(msg==-6){ 
                        alert(' The password was not entered correctly '); 
                        $("#password").focus(); 
                    } 
                    if(msg==1){ 
                        //Alert (' login successful ');
                        window.location.href='./index.php'; 
                    } 

                } 
            }); 
    }); 

});
 

Related articles: