wordpress - 重力形式预选选项复选框字段
问题描述
我正在使用带有一个单选按钮和一个复选框字段的重力表单,它们都有相同的选择。我需要的是当我的用户在单选按钮上选择一个选项时,该选项会自动在复选框字段中预先选择。他不必再次选择它。
我看到我必须使用一些过滤器,例如下面的过滤器,但我无法成功自定义它以完成我需要的操作。
如果有人能帮助我,他会救我的命。
<?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。
对于需要这样做的其他人,我们已经编写了有关该过程的完整演练以及易于理解的用例。
这是一个快速的速成课程:
创建映射表
创建一个表单,用于跟踪在选择给定选项(在另一个字段中)时应选中哪些复选框。
提交表单条目
现在您将完成一些数据输入。选择触发器选项,然后选择应为该选项选中的每个复选框。
复制映射表
接下来,您将根据映射表单创建一个新表单。这是您的用户将实际与之交互的表单。映射表只适合您。
映射您的字段
最后,使用Populate Anything根据所选选项动态检查(即填充值)复选框字段,并从映射表单中检查该选项的复选框。
推荐阅读
- assembly - 尝试编译 asm 代码时重定位截断错误
- sql - 选择为我提供未接受特定培训的讲师的声明
- ios - Swift 5 - UIButtons 在 tableView 页脚与原始分隔线并排
- css - 为什么我的按钮位于导航栏中的“工作流程”下方?
- mysql - MySQL 表显示异常
- java - 如何设置 FailureSummaryLoggingAppenderListener 以便我知道我的异步 appender 何时删除了日志消息
- froala - 当我将 froala 编辑设置为“关闭”时,编辑器中的链接停止工作
- python - 当值是 Pandas 数据框中的数字时,将字符串转换为整数时出错
- spring-boot - 使用带有驼峰式前缀的 ConfigurationProperties 时,如何解决“前缀必须采用规范形式”?
- c# - 如何在excel VTO插件中重新创建按钮