重力形成预选选项复选框字段



我使用的Gravity表单有一个单选按钮和一个复选框字段,它们都有相同的选择。我需要的是,当我的用户在单选按钮上选择一个选项时,这个选项会在复选框字段中自动预先选择。他不必再选择它。

我看到我必须使用一些过滤器,例如下面的过滤器,但我无法成功地自定义它来做我需要的事情。

如果有人能帮助我,他就会救我的命。

<?php
# Make sure to replace {id} with your form's id
add_filter( 'gform_pre_render_{id}', 'my_populate_checkbox' );
function my_populate_checkbox( $form ) {
/**
* Loop through form fields
*
* Note we are using the `$field` array as a direct reference using `&`. 
* This means that changing its value will within the loop will 
* change the corresponding `$form` array item
*/
foreach( $form['fields'] as &$field ) {
# Make sure to change `1` to the ID of the checkbox field that you want to pre-populate
if( 1 === $field->id ) {
/**
* Loop through the choices for this checkbox
*
* Note again that we are passing `$choice` by reference in order to change the 
* corresponding array item within the `$field` array
*/
foreach( $field->choices as &$choice ) {
/**
* If this choice has a value of 'red' or 'blue', then make sure the checkbox is pre- 
checked
* by setting the `isSelected` parameter to `true`
*/
if( 'red' === $choice['value'] || 'blue' === $choice['value'] ) {
$choice['isSelected'] = true;
}
} # end foreach: $field->choices
} # end if: $field->id equals 1
} # end foreach: $form['fields']
# return the altered `$form` array to Gravity Forms
return $form;
} # end: my_populate_checkbox

很高兴这对你有用,Jean。

对于其他需要这样做的人,我们已经编写了一个完整的流程演练,以及一个易于遵循的用例。

如何有条件地检查复选框(和其他基于选择的字段(

这里有一个快速速成课程:

  1. 创建映射表单

    创建一个表单,该表单将用于跟踪在选择给定选项(在另一个字段中(时应选中的复选框。

  2. 提交表单条目

    现在,您将浏览并进行一些数据输入。选择触发器选项,然后选中该选项应选中的每个复选框。

  3. 复制映射表

    接下来,您将基于映射表单创建一个新表单。这是用户实际交互的表单。映射表只适合您。

  4. 绘制您的领域

    最后,使用Populate Anything根据所选选项和映射表单中为该选项选中的复选框动态检查(即填充值(复选框字段。

感谢Gravity Wiz团队回答了我的问题。这里有一个视频来解释如何做到这一点:https://www.loom.com/share/f20184e287be49e89116d4c641c77c11

想法很简单:1-你必须安装Gravity Perks:填充任何东西2-您必须映射两个表单:一个带有数据,另一个将接收数据(此表单包含您的表单(3-这两个表单必须具有相同的字段4-在映射表上,你必须创建条目,对应于你的需求

这有点棘手,但对我来说效果很好

最新更新