×

用jQuery实现按回车提交数据

作者:jiang2017.08.03来源:Web前端之家浏览:8491评论:0
关键词:JQueryJS
其实jquery按回车提交数据是很简单的事情,我们只要检查测到用户按了回车就直接绑定事件.click就实现了提交了,在按钮上我们绑定ajax提交表单事件即可。
$(document).ready(function(){ 
    $("按下回车的控件").keydown(function(e){ 
        var curKey = e.which; 
        if(curKey == 13){ 
        $("#回车事件按钮控件").click(); 
            return false; 
        } 
    }); 
});

是用js的ajax功能同时支持回车事件。

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

您的支持是我们创作的动力!
温馨提示:本文作者系 ,经Web前端之家编辑修改或补充,转载请注明出处和本文链接:
https://www.jiangweishan.com/article/svg1488211200974.html

网友评论文明上网理性发言 已有0人参与

发表评论: