Gravity Forms - 获取当前页面编号

10
我有一个多页表单。
我想在这个表单的最后一页执行一些自定义JavaScript。理论上,我只需要检索当前页面编号并编写条件即可。
简单吧?显然不是这样。
我的原始解决方法如下:
if ($('gform_page').last().css('display') !== 'none') {
    // perform custom scripts now that the last
    // Gravity Form page is being shown
}

但是,在表单中我尝试过的每个元素上,$('...').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
});

除了没有正确的代码外,我还怀疑我没有将代码放在正确的位置,因为我也在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);
?>

问题:

我需要什么代码来获取当前页面的页码,更重要的是,我需要将该代码放在哪里?


关闭问题不是为了这个目的。问题会因为离题或由于缺少信息而无法回答而被关闭。这是问题存在问题的标志。如果您不想再与问题互动,只需忽略它即可。 - Modus Tollens
9个回答

8

虽然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
    }
});

当然,您可以使用formId参数将其限制为特定的表单。


我可以将用户重定向到上一页吗?上一页的URL是什么? - Parthavi Patel

8

我明白了。

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');

如果你复制/粘贴以上代码,请确保:

  • 10 替换为你想要检查的页面
  • wp_enqueue_script 中确保你的参数正确
  • 63 替换为你的表单 ID

一些我发现有用的资源:


1
我认为上面McNab的回答更好。 :) - Chris Rae
1
如果有人先点击了“上一页”按钮,然后再次点击“下一页”,那么页面数字就会偏移1,这个功能无法正常工作。 - Enchiridion

2

目前被接受的答案仅在用户未返回表单中的上一页时有效,如果他们这样做,那么gform_sourge_page_number总是落后于一。我找到了一个更好的解决方案(使用此钩子作为示例,但您应该能够在任何传递给它的钩子中使用它):

function run_script_on_last_page( $form)  {
  if ( !is_admin() ) {
    if ( \GFFormDisplay::get_current_page( $form['id'] ) == 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' );

GFFormDisplay::get_current_page( $form_id )是许多方便但未记录在案的函数之一。


1
我编写了一个小函数,可返回当前页面:
// Get Gravity Forms Current Page
// Syntax: gf_current_page()
function gf_get_current_page()
{
    return rgpost('gform_source_page_number_' . $_POST['gform_submit']) ? rgpost('gform_target_page_number_' . $_POST['gform_submit']) : 1;
}

0

除了已接受的答案,这里提供一个示例来查找当前页面,并动态查找表单中有多少页。该示例根据是否为表单的最后一页更改按钮文本,而不是将脚本排队。

// Change the text of the 'Next' form button to 'Submit' when
// the current page is the final page of the form
add_filter( 'gform_next_button', 'next_to_submit', 10, 2 );
function next_to_submit( $next_button, $form ) {
    $replacement_next_button = $next_button;
    $last_page_number = 1;
    foreach ($form['fields'] as $field) {
        $is_hidden = RGFormsModel::is_field_hidden( $form, $field, array() );
        if ($field['pageNumber'] > $last_page_number && !$is_hidden) {
            $last_page_number = $field['pageNumber'];
        }
    }
    $current_page_number = rgpost('gform_source_page_number_' . $form['id']) ? rgpost('gform_source_page_number_' . $form['id']) : 1;
    if ($last_page_number === $current_page_number) {
        // If the current page is the final page
        $replacement_next_button = str_replace( 'Next', 'Submit', $next_button );
    }
    return $replacement_next_button;
}

0

试着使用这个:

var current_visible_page = jQuery('.gf_step_active .gf_step_number').html();
console.log(current_visible_page);

在控制台中使用上述方法来检查它的工作方式。


0
对于任何在PHP中寻找答案的人。
您可以使用GFFormDisplay类在PHP中获取当前页面。
示例:
\GFFormDisplay::get_current_page({$form_id})
从Gravity Forms插件(第652行)检索到的信息。
如果您想要更多关于GFFormDisplay类的信息,请查看下面链接: https://github.com/wp-premium/gravityforms/blob/master/form_display.php

0
使用 Jquery
  var current_page_ = 1+parseInt(jQuery('.gform_page:visible').index());

0

以下代码在JavaScript中适用:

$('.gf_step_active .gf_step_number').html()

它将会给你当前多页表单中的页面编号。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接