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

我正在尝试使用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"); }); 

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

 wpcf7.initForm 

请改用此function:

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

onAfter调用它,它应该解决您的问题。

以下是Contact Form 7支持论坛的讨论: https : //wordpress.org/support/topic/init-function-wpcf7initform/