【问题标题】:success.form.bv fires multiple timessuccess.form.bv 多次触发
【发布时间】:2018-12-16 07:17:55
【问题描述】:

我真的需要一些帮助来解决使用 BootstrapValidator 的表单提交问题。

我有以下代码使用 AJAX 调用页面,当我获得成功响应时,它会使用 location.reload(true) 刷新页面。

$('#indstillingForm1')
  .on('success.form.bv', function(e) {
    // Prevent form submission
    e.preventDefault();
    // Get the form instance
    var $form = $(e.target);
    // Get the BootstrapValidator instance
    var bv = $form.data('bootstrapValidator');
    $.ajax({
      url: "../bin/gem_indstilling.php",
      type: "POST",
      processData: false,
      contentType: false,
      dataType:"json",
      data: form_data,
      cache: false,
      success: function(response) {  
      if(response.result === "success") {
        $('html, body').animate({scrollTop:0}, 'slow');
        setTimeout(function(){
          location.reload(true);
        }, 2000);
      } else if(response.result === "failure") {
      }
    })
  });
}); 

问题是.on('success.form.bv') 代码被多次触发。

我该如何解决这个问题?

【问题讨论】:

    标签: jquery ajax bootstrapvalidator


    【解决方案1】:
     if(response.result === "success") {
            $('html, body').animate({scrollTop:0}, 'slow');
            setTimeout(function(){
              location.reload(true);
            }, 2000);
          }
    

    我想这是你重新加载页面导致再次发送相同请求的问题

    【讨论】:

    • 但是 .on('success.form.bv') 只有在提交表单 #indstillingForm1 时才会被触发。不是吗?并且 location.reload 没有提交表单。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多