Home >Web Front-end >JS Tutorial >jquery code example to submit data by pressing enter_jquery

jquery code example to submit data by pressing enter_jquery

WBOY
WBOYOriginal
2016-05-16 17:17:341087browse

In fact, it is very simple for jquery to press enter to submit data. We only need to check that the user pressed enter and bind the event directly. Click will realize the submission. On the button, we can bind the ajax submit form event.
Core code

Copy code The code is as follows:

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

is using js The ajax function also supports carriage return events
Copy code 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_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('用户名不能为空');
                        $("#username").focus();
                    }
                    if(msg==-2){
                        alert('用户名不存在');
                        $("#username").val("");
                        $("#username").focus();
                    }
                    if(msg==-3){
                        alert('密码不能为空');
                        $("#password").focus();
                    }
                    if(msg==-6){
                        alert('密码输入不正确');
                        $("#password").focus();
                    }
                    if(msg==1){
                        //alert('登录成功');
                        window.location.href='./index.php';
                    }

                }
            });
    });

});
 
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn