You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何禁用Gravity Forms多页表单按钮的书签导航跳转行为?

禁用Gravity Forms多页表单的页面顶部跳转行为

方法1:用JavaScript拦截锚点跳转

Gravity Forms的多页按钮默认会添加带锚点的链接(比如#gform_target_page_1),点击时会触发浏览器锚点定位跳转到页面顶部。可以用jQuery阻止这个默认行为:

jQuery(document).ready(function($) {
    // 拦截所有多页表单的下一步/上一步按钮点击
    $(document).on('click', '.gform_next_button, .gform_previous_button', function(e) {
        // 阻止锚点跳转
        e.preventDefault();
        // 手动触发Gravity Forms原生的页面切换逻辑
        $(this).closest('form').find('#gform_target_page_' + $(this).data('target')).trigger('click');
    });
});

把这段代码添加到主题functions.php(通过wp_enqueue_script挂载),或者直接放到WordPress后台「外观」→「自定义」→「额外JavaScript」区域。

方法2:用PHP钩子修改按钮链接

通过Gravity Forms的gform_pre_render钩子,直接移除按钮的锚点href属性:

add_filter('gform_pre_render', 'remove_gform_page_jump');
function remove_gform_page_jump($form) {
    foreach ($form['fields'] as &$field) {
        // 定位分页按钮字段
        if ($field->type == 'page') {
            // 清空下一步按钮的锚点链接
            if (!empty($field->nextButton)) {
                $field->nextButton['attributes']['href'] = '';
            }
            // 清空上一步按钮的锚点链接
            if (!empty($field->previousButton)) {
                $field->previousButton['attributes']['href'] = '';
            }
        }
    }
    return $form;
}

将这段代码粘贴到当前主题的functions.php文件中即可。

注意事项

  • 方法1依赖jQuery,大部分WordPress主题默认已加载,若未加载需手动引入。
  • 修改代码后记得清空浏览器缓存,使用缓存插件的话也要刷新站点缓存。

内容的提问来源于stack exchange,提问作者anon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 07:18:19