重力形式-获得当前页码
Gravity Forms - Get Current Page Number
我有一个多页的表单。
我想在这个表单的最后一页执行一些自定义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_render
和gform_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
});
除了没有正确的代码,我还怀疑我没有在正确的地方的代码,因为我也徒劳地尝试了以下function .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);
?>
<标题>问题:我需要什么代码来检索当前的页码,更重要的是,我应该把这些代码放在哪里?
标题>我明白了。
函数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
一些我发现有用的资源:
-
gform_validation
过滤器的这部分文档 -
gform_enqueue_scripts
.
OPs接受的答案可能很好地工作,但如果您使用Ajax设置了表单分页,则无法工作。
在这种情况下,我只能使用Javascript和以下方法;
http://www.gravityhelp.com/documentation/page/Gform_post_renderjQuery(document).bind('gform_post_render', function (event, formId, current_page) {
if (current_page == 2) {
// do something
}
});
您当然可以使用formId
参数将此限制为特定的表单
当前接受的答案只有在用户从未进入表单的前一页时才有效,如果他们这样做,则gform_sourge_page_number
总是滞后1。我发现了一个更好的解决方案(使用这个钩子作为一个例子,但你应该能够在任何钩子中使用它,有$form传递给它):
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 )
是众多方便的未记录函数之一。
我写了一个返回当前页面的小函数:
// 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;
}
除了可接受的答案之外,这里还有一个查找当前页面并动态查找表单中有多少页面的示例。此示例根据是否为表单的最后一页来更改按钮文本,而不是将脚本加入队列。
// 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;
}
以下代码在JavaScript中为我工作:
$('.gf_step_active .gf_step_number').html()
以多页形式显示当前页面的页码。
使用Jquery
var current_page_ = 1+parseInt(jQuery('.gform_page:visible').index());
试试这个:
var current_visible_page = jQuery('.gf_step_active .gf_step_number').html();
console.log(current_visible_page);
在控制台中使用上面的方法来检查它是如何工作的
在PHP中寻找答案的人
您也可以使用GFFormDisplay
类在PHP中获取当前页面。
的例子:
'GFFormDisplay::get_current_page({$form_id})
从Gravity Forms插件中检索到的信息(第652行)。
如果您想了解更多关于GFFormDisplay
类的信息,请查看以下内容:https://github.com/wp-premium/gravityforms/blob/master/form_display.php
- 获取当前页面的URL并将其附加到Facebook共享链接(使用javascript)
- 如何在 safari 中获取当前页面的 url 和标题
- Chrome扩展程序将外部JavaScript添加到当前页面的html中
- 在服务器更改时自动更新/重新加载当前页面的库
- 检测具有 href 属性的“A”标记,该属性等于当前页面的 URL
- 如何定义相对于当前页面的 URL w jQuery
- 我想通过javascript生成当前页面的url,并添加两个参数
- 从当前页面的URL中删除哈希
- 如何在手风琴导航菜单中设置当前页面子链接及其父链接的样式
- Javascript获取当前页面的html
- 如何在文本输入中显示当前页面的url,供用户复制
- 如何使用Jquery和Javascript在当前页面的Div中显示错误消息
- jQuery:添加Class Active到<与当前页href
- 如何将当前页内容追加到另一页
- 解析当前页面的html
- 使用javascript在重载时更改当前页面的URL
- 如何在不刷新页面的情况下将当前页面的url发送给servlet
- 如何提交表单并在当前页面的Div中加载目标页面
- 如何在Firefox插件中获取当前页面的SSL证书信息?
- Ng-table, ng-repeat,过滤器只对分页表中的当前页起作用