重力形式 - 获取当前页码

gme*_*ben 6 javascript php wordpress wordpress-plugin

我有一个多页表格.

我想在此表单的最后一页上执行一些自定义JavaScript.从理论上讲,我所要做的就是检索当前页码并编写条件.

简单吧?显然不是.

我原来的解决方法是这样的:

if ($('gform_page').last().css('display') !== 'none') {
    // perform custom scripts now that the last
    // Gravity Form page is being shown
}
Run Code Online (Sandbox Code Playgroud)

$('...').css('display')回报率undefined每一个元素我试过这个表单内.每次用户点击"下一步"按钮时,都会触发自定义脚本.没有雪茄.

然后,在查看Gravity Forms文档后,我发现了两个看似有用的事件:gform_post_rendergform_page_loaded.

但是,文档未提供有关如何访问参数的说明.

jQuery(document).bind('gform_page_loaded', function(event, form_id, current_page){
    console.log(current_page);
    // returns nothing when loaded in the footer
    // returns [Object, object] when placed in an HTML field in the form
});
Run Code Online (Sandbox Code Playgroud)

除了没有正确的代码之外,我还怀疑我没有正确的代码,因为我还在functions.php和header.php中尝试了以下内容(如文档所示):

<?php
function enqueue_custom_script($form, $is_ajax){
    if ($is_ajax) :
        echo '<script>console.log(current_page);</script>';
    endif;
}
add_action("gform_enqueue_scripts", "enqueue_custom_script", 10, 2);
?>
Run Code Online (Sandbox Code Playgroud)

题:

我需要什么代码来检索当前页码,更重要的是,我在哪里放置该代码?

gme*_*ben 6

我知道了.

rgpost显然,该功能对于访问当前页码至关重要.经过我自己的一些混乱之后,我能够在functions.php和header.php中的wp_head()函数之前获得以下代码.

function run_script_on_last_page($form) {
    if (!is_admin()) {
        $current_page = rgpost('gform_source_page_number_' . $form['id']) ? rgpost('gform_source_page_number_' . $form['id']) : 1;
        if ($current_page == 10) {
            wp_enqueue_script('custom_script', get_template_directory_uri() . '/js/custom_script.js', array('jquery'), null, true);
        }
    }
}
add_action('gform_enqueue_scripts_63', 'run_script_on_last_page');
Run Code Online (Sandbox Code Playgroud)

如果您要复制/粘贴上述代码,请务必:

  • 替换10为您要检查的页面
  • 确保您的参数正确无误 wp_enqueue_script
  • 替换63为您的表单ID

我认为有用的一些资源:


McN*_*Nab 6

OP接受的答案可能会有效,但是如果您使用Ajax进行分页表单设置则不起作用.

在这种情况下,我只能使用Javascript和以下方法工作;

http://www.gravityhelp.com/documentation/page/Gform_post_render

jQuery(document).bind('gform_post_render', function (event, formId, current_page) {
    if (current_page == 2) {
        // do something
    }
});
Run Code Online (Sandbox Code Playgroud)

您当然可以使用该formId参数将其限制为特定形式