bootstrapValidator校验后,ajax重复提交问题解决

文章讲述了在网页表单中遇到的Ajax提交被多次触发以及与BootstrapValidator验证插件冲突的问题。通过将提交按钮类型改为button,然后在点击事件中添加验证和Ajax提交的逻辑,成功解决了这两个问题。修改后的代码确保了只有当表单验证通过时才会执行Ajax提交操作。
摘要由CSDN通过智能技术生成

参考文章

ajax的重复提交问题,和bootstrapValidator校验冲突问题解决办法

问题代码

  <div class="modal-footer">
      <button type="submit" class="btn btn-green btn-sm">新增</button>
  </div>



<script type="text/javascript">
var fields = {
        password: {validators: {notEmpty: {message: '登录密码不能为空'}}},
        //其余校验...
}
$('#serviceForm').bootstrapValidator({
    feedbackIcons: {
        valid: 'glyphicon glyphicon-ok',
        invalid: 'glyphicon glyphicon-remove',
        validating: 'glyphicon glyphicon-refresh'
    },
    fields: fields
}).on("success.form.bv",function(e){
    e.preventDefault();
	 //ajax提交的代码...
});

</script>

改后代码

<div class="modal-footer">
     <button type="button" class="btn btn-green btn-sm" id="reportButton">新增</button>
 </div>


<script type="text/javascript">
    var form = $('#serviceForm');
    var fields = {
        password: {validators: {notEmpty: {message: '登录密码不能为空'}}},
        //其余校验...
    }
    
    $(document).ready(function () {
        form.bootstrapValidator({
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: fields
        })
    });

    $("#reportButton").click(function (e) {
        var bmz= form.data('bootstrapValidator');
        bmz.validate();
        if (bmz.isValid()) {
            //ajax提交的代码...
        }
    })


</script>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值