首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >重力形成基于所选值数量的条件逻辑

问重力形成基于所选值数量的条件逻辑
EN

Stack Overflow用户
提问于 2021-09-21 13:44:12
回答 2查看 173关注 0票数 0

我有一个用重力形式构建的表单,它问5个问题,每个问题有2个答案。

每个答案对应于一个类别,所以我希望能够添加一些条件逻辑,以便确认发送到不同的页面,这取决于哪个类别有更多的答案。

表格在这里:https://90daysfromretirement.com/medicare-advantage-vs-medicare-supplement/

这两个类别是“优势”和“补充”-因此取决于答案,所以无论哪个类别有更多的响应(3个或更多),则确认会将它们重定向到相应的页面。

问题是我不知道如何计算每一个的值并运行它的逻辑。我认为这是不可能的任何重力形式的附加组件,我已经找到,我还不能把代码放在一起来做。

有什么建议吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-09-22 14:33:25

因为你只有两个选项,你真的只需要知道一个问题的计数和问题的总数,就可以知道哪个值被选择得更多。

例如,如果您有10个问题,并且选择了6次" Advantage“选项,那么您就知道Advantage不止是"Supplement”。

这是一个可以帮助您入门的代码片段。它将采用一组单选按钮字段,计算选择指定值的次数,并将该计数填充到数字字段中。

然后,您可以使用该数字字段对您的确认应用条件逻辑,以重定向到相应的页面(例如,如果计数大于5,则重定向到Advantage页面。如果计数小于6,则重定向到补充页面)。

代码语言:javascript
复制
/**
 * Gravity Wiz // Gravity Forms // Value Counter
 * https://gravitywiz.com/
 * 
 * Count the number of times a given value has been selected in a group of fields and populate that number into a Number field.
 * This snippet is designed to target a Number field and count selected values in Checkbox and Radio Button fields.
 * 
 * This snippet works best with our free [GF Custom Javascript](https://gravitywiz.com/gravity-forms-custom-javascript/) plugin.
 */
// Replace the "1", "2" and "3" with field IDs of fields that should have their selected values counted. If you are using the 
var $radios    = jQuery( '#field_GFFORMID_1, #field_GFFORMID_2, #field_GFFORMID_3' );
// Replace "4" with the ID of the Number field in which the count should be populated.
var $target    = jQuery( '#field_GFFORMID_4' );
// Replace "a" with the value you wish to count if selected.
var countValue = 'a';

function gwRecountValues() {
    $target.find( 'input' ).val( $radios.find( 'input:checked[value="' + countValue + '"]' ).length );
}

$radios.on( 'change', function() {
    gwRecountValues();
} );

gwRecountValues();

这是源代码,如果其他人在使用它时遇到任何问题或需要改进,它可能会在未来更新:https://github.com/gravitywiz/snippet-library/commit/d34eb169da937981c4a1ea49bb8a36df023bff1b

这段代码中缺少的一件事是PHP验证。您可能不关心人们更改他们的计数,但这是可能的,而不需要PHP验证。

票数 1
EN

Stack Overflow用户

发布于 2021-09-24 01:08:27

您可以尝试将以下内容添加到functions.php文件中

代码语言:javascript
复制
add_action( "gform_after_submission_66", "after_submission", 10, 2 ); //replace 66 with your form number
    function after_submission($entry, $form ){
        $supplement_answers = array("milk", "apple", "pasta", "fries", "oats"); //replace array items with the supplement answers
        $advantage_answers = array("red", "blue", "pink", "purple", "yellow"); //replace array items with the advantage answers
        $field_nums = array("2","3","6","7","9"); //replace these field numbers with your own -- order then to align with answers above
        $count_fields = count($field_nums);
        $a = 0;
        $b = 0;
        for($i=0; $i<$count_fields; $i++){
            if($entry[$field_nums[$i]] == $supplement_answers[$i]){
                $a = $a + 1;
            }else if($entry[$field_nums[$i]] == $advantage_answers[$i]){
                $b = $b + 1;
            }
        }
        if($a > $b){
             header('Location: https://website/supplements_page'); //replace url
        }else if($a < $b){
             header('https://website/advantage_page'); //replace url -
        }
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69269946

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档