• <button>会自动提交表单吗?


    点击button以后,表单先由ajax提交,然后无论后台返回什么结果,页面都会跳转到表单action属性指定的路劲,
    也就是login.html
    使用的是html、jquery、javascript,后台是spring mvc 代码如下:

    html表单:

    XML/HTML code
     
    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    <form action="login.html" method="post" id="loginForm">
            <input type="text" id="username" name="userName" />
            <input type="password" id="password" name="password" />
            <button class="btn btn-warning btn-loginsize" onClick="submitForm()">
                登陆
            </button>
            <a href="#">忘记密码?</a>
            <a href="#">注册</a>
    </form>



    jquery、javascript代码:

    JavaScript code
     
    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    <script>
        function submitForm() {
            $.ajax({
                cache: true,
                type:"POST",
                url: "verification.json",
                dataType:"json",
                data:$("#loginForm").serializeArray(),
                async: false,
                success:function(data){
                    if(data.flag == "1") { //登陆成功
                        alert("aa"); //这里返回正确
                        location.href="index.html";//这里没有作用,什么原因?
                    }else//登陆失败
                        alert("warnings");
                    }
                },
                error:function(data){
                    alert("error");
                }
            });
        }
    </script>




    后台代码

     
     
    Java code
     
    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    @RequestMapping("/verification.json")
    @ResponseBody
    public Map<String, Object> verification(User user) {
        Map<String, Object> map = new HashMap<String, Object>();
         
        if (user.getUserName().equals("admin") && user.getPassword().equals("admin")) {
            map.put("flag""1");
        else {
            map.put("flag""0");
        }
        return map;
    }
    button请始终为按钮规定 type 属性。Internet Explorer 的默认类型是 "button",而其他浏览器中(包括 W3C 规范)的默认值是 "submit"。
  • 相关阅读:
    CentOS编译安装OpenCV fang
    Ubuntu下编译安装OpenCV fang
    OpenCV4 轮廓检测 快速入门 fang
    CentOS编译安装Nginx fang
    echarts图中动态加载、悬浮框(tooltip)中嵌套echarts
    oracle批量插入数据的实验有一个注意细节
    oracle中 ORA01950: 对表空间 'K3CLOUD_DATA' 无权限 报错处理
    Linux中 du 命令,查看目录大小
    把excel中的数据导入到oracle数据库中的操作步骤比较常用
    数字证书编码ASN.1
  • 原文地址:https://www.cnblogs.com/anruy/p/5150858.html
Copyright © 2020-2023  润新知