Ajax加载中断防止默认

时间:2018-02-19 17:01:31

标签: jquery ajax

我有这个脚本来编辑带有ACF表单的WP帖子。

成功之后,我正在重新加载内容,但是下面这只是交替工作

所以第一次,在提交时,数据保存,然后内容重新加载正常。在第二次数据保存但随后整个页面重新加载,好像它打破了防止默认值。

我知道我需要在每次提交和加载后重置或执行类似的操作,但无法解决这个问题。

由于

(function($) {
    //// Get the current page

    var pathtopage = window.location.pathname;

    //// Save data on submit

    acf.do_action('ready', $('body'));
    $('form#modalAjaxTrying').on('submit', function(e) {
        e.preventDefault();
    });
    acf.add_action('submit', function($form) {
        $.ajax({
            url: window.location.href,
            method: 'post',
            data: $form.serialize(),
            success: function(data) {
                 acf.validation.toggle($form, 'unlock');

                //// On Success reload content

                $('#content').load(pathtopage + ' #content');
           } 
        });
    });

})(jQuery);

1 个答案:

答案 0 :(得分:1)

ACF有什么用?为什么不呢

function() {
  var pathtopage = window.location.pathname;

  $('form#modalAjaxTrying').on('submit', function(e) {
    e.preventDefault();
    $.ajax({
      url: window.location.href,
      method: 'post',
      data: $form.serialize(),
      success: function(data) {
        acf.validation.toggle($form, 'unlock');
        $('#content').load(pathtopage + ' #content'); // this also boggles the mind
      }
    });
  });

})

但是为什么当你“重新加载”页面时,第一页中的Ajax呢? 此外,如果新内容包含表单,则您将丢失事件处理程序并需要delegate

相关问题