通过AJAX(smoothstate.js)加载页面时,联系表单7抛出'wpcf7.initForm不是函数'错误

时间:2017-08-16 17:03:12

标签: jquery ajax wordpress contact-form-7 smoothstate.js

我正在尝试使用smoothstate.js在我的wordpress网站上实现Contact Form 7。当直接加载使用它的页面时,联系表单可以正常工作。但是,如果通过AJAX加载页面,则会出现错误'wpcf7.initForm不是函数'。

我不是AJAX的天才,但我的想法是在AJAX onAfter函数中重新初始化。我试过使用wpcf7InitForm();但仍然没有运气。

非常感谢有关此主题的任何帮助!

这是我目前的AJAX代码:

  //SmoothState Page Transitions

  $(function(){
    'use strict';
    var $page = $('#main'),
        options = {
          debug: true,
          prefetch: true,
          onStart: {
            duration:  800, // Duration of our animation
            render: function ($container) {
              // Add your CSS animation reversing class

              $container.addClass('is-exiting');

              // Restart your animation
              smoothState.restartCSSAnimations();
            }
          },
          onReady: {
                duration: 0,
                render: function($container, $newContent) {
                    // Remove your CSS animation reversing class
                    $container.addClass('is-loaded');

                    setTimeout(function(){
                      $container.addClass('unload');
                    }, 600);

                    setTimeout(function(){
                      $container.removeClass('is-loaded unload');
                    }, 900);


                    // Inject the new content
                    $container.html($newContent);
                }
            },
            onAfter: function($container) {
              $container.removeClass('is-exiting');
               $('div.wpcf7 > form').wpcf7InitForm();
               $(window).data('plugin_stellar').refresh();
            }
        },
        smoothState = $("#main").smoothState(options).data("smoothState");
      });

1 个答案:

答案 0 :(得分:6)

v4.8中的联系表单7发生了更改,摆脱了jquery.form.js,因此wpcf7InitForm()函数不再起作用。但是,在v4.8.1中引入了一个新的init函数

wpcf7.initForm

请改用此功能:

function initContactForm() {
 $( 'div.wpcf7 > form' ).each( function() {
  var $form = $( this );
  wpcf7.initForm( $form );
  if ( wpcf7.cached ) {
   wpcf7.refill( $form );
  }
 });
}

onAfter中调用它,它可以解决您的问题。

以下是关于联系表格7支持论坛的讨论: https://wordpress.org/support/topic/init-function-wpcf7initform/