• JavaScript解决一个带验证的Form两个Submit事件(一个页面保持不动【AJAX实现】,一个页面提交并跳转)的场景


    <form class="form-horizontal" action="/biz/patent/edit" method="post" enctype="multipart/form-data" data-toggle="validator" role="form" novalidate="true" id="patentEditForm"> 
    <button class="btn btn-primary" type="button" id="but_sub">保存</button> <button class="btn btn-primary" type="button">保存返回列表</button> <button class="btn btn-warning" type="button" onclick="window.location='/biz/project/list';">返回</button>
    </form>
    $(document).ready(function(){
        var buttonId;
    
        $('#patentEditForm').validator().on('submit', function (e) {
            if (e.isDefaultPrevented()) {
                // handle the invalid form...
            } else {
                // everything looks good!
                if(buttonId == "but_sub"){
                    var formData = new FormData($("#patentEditForm")[0]);
                    $.ajax({
                        type:"POST",
                        url:"/biz/",
                        data: formData,
                        dataType:"JSON",
                        nsync:false,
                        cache: false,
                        contentType: false, 
                        processData: false,
                        success:function(date){
                            switch(date.status){
                            case 1:
                                $success("保存成功!");
                                break;
                            case 1009:
                                $error("保存失败!");
                                break;
                            default :
                                $error("没有提交信息");
                            }
                        },
                        error:function(){
                            $error("没有提交信息");
                        }
                    });
    
                    return false;
                }
            }
        });
    
        $(".btn-primary").click(function(){
            buttonId = $(this).attr("id");
            $('#patentEditForm').trigger('submit');
        });
    1.$(".btn-primary").click()和 $('#patentEditForm').validator().on('submit')两个事件的处理方法之间传递参数(用全局变量)。
    2.不用原来的表单提交,用.trigger('submit')方法在JavaScript里触发提交。


  • 相关阅读:
    open-falcon实现邮件报警
    open-falcon监控Flume
    Ubuntu下安装open-falcon-v0.2.1
    Python学习笔记——发邮件
    Flume的监控参数
    Ubuntu下安装Kafka Manager
    Ubuntu系统监控indicator-sysmonitor
    kafka性能测试
    Ubuntu下安装sbt
    Ubuntu安装shadow$ocks-libev
  • 原文地址:https://www.cnblogs.com/rgqancy/p/6878397.html
Copyright © 2020-2023  润新知