【问题标题】:Gravity Forms - How to move radio input inside the label element重力形式 - 如何在标签元素内移动无线电输入
【发布时间】:2020-08-25 00:21:51
【问题描述】:

我正在使用带有Quiz Addon 的 WordPress Gravity Forms,并且我想编辑测验答案的无线电输入的 HTML。

这是 Gravity 表单当前为测验答案的每个 li 元素呈现的内容:

<li class="gchoice_3_1_1">
    <input name="input_1" type="radio" value="gquiz1c4448d0c" checked="checked" id="choice_3_1_1" tabindex="2">
    <label for="choice_3_1_1" id="label_3_1_1" class="gquiz-correct-choice">Example label text
        <img class="gquiz-indicator" src="https://www.example.com/wp-content/plugins/gravityformsquiz-master/images/tick.png" alt="Correct response">
    </label>
</li>

这就是我想要实现的(将input 元素移动到label 字段中):

<li class="gchoice_3_1_1">
    <label for="choice_3_1_1" id="label_3_1_1" class="gquiz-correct-choice">Example label text
        <input name="input_1" type="radio" value="gquiz1c4448d0c" checked="checked" id="choice_3_1_1" tabindex="2">
        <img class="gquiz-indicator" src="https://www.example.com/wp-content/plugins/gravityformsquiz-master/images/tick.png" alt="Correct response">
    </label>
</li>

这是我在子主题functions.php 中使用的 PHP 函数来尝试实现它:

// move radio input inside label
add_filter("gform_field_choices", "radio_input_inside_label", 10, 2);
function radio_input_inside_label($choices, $field){
    if($field["type"] != "radio")
        return $choices;

    $choices = "";

    if(is_array($field["choices"])){
        $choice_id = 0;
        $count = 1;

        $logic_event = !empty($field["conditionalLogicFields"]) ? "onclick='gf_apply_rules(" . $field["formId"] . "," . GFCommon::json_encode($field["conditionalLogicFields"]) . ");'" : "";

        foreach($field["choices"] as $choice){
            $id = $field["id"] . '_' . $choice_id++;
            $field_value = !empty($choice["value"]) || rgar($field, "enableChoiceValue") ? $choice["value"] : $choice["text"];
            $checked = rgar($choice,"isSelected") ? "checked='checked'" : "";
            $tabindex = GFCommon::get_tabindex();

            $input = sprintf("<input name='input_%d' type='radio' value='%s' %s id='choice_%s' $tabindex $logic_event />", $field["id"], esc_attr($field_value), $checked, $id);
            $choices .= sprintf("<li class='gchoice_$id'><label for='choice_%s'>%s %s</label></li>", $id, $choice["text"], $input);

            $count++;
        }

    }

    return $choices;
}

此函数适用于标准 Radio 输入,它将 input 移动到 label 内。但它不适用于测验无线电输入。

你能看出哪里不对吗?

【问题讨论】:

    标签: php gravity-forms-plugin gravityforms


    【解决方案1】:

    我使用了错误的字段类型选择器。

    如果您想将单选输入移动到 NORMAL 重力表单单选按钮的标签元素内,则此代码将起作用:

    // Gravity Forms Quiz - move radio input inside label
    add_filter("gform_field_choices", "radio_input_inside_label", 10, 2);
    function radio_input_inside_label($choices, $field){
        if($field["type"] != "radio")
            return $choices;
    
        $choices = "";
    
        if(is_array($field["choices"])){
            $choice_id = 0;
            $count = 1;
    
            $logic_event = !empty($field["conditionalLogicFields"]) ? "onclick='gf_apply_rules(" . $field["formId"] . "," . GFCommon::json_encode($field["conditionalLogicFields"]) . ");'" : "";
    
            foreach($field["choices"] as $choice){
                $id = $field["id"] . '_' . $choice_id++;
                $field_value = !empty($choice["value"]) || rgar($field, "enableChoiceValue") ? $choice["value"] : $choice["text"];
                $checked = rgar($choice,"isSelected") ? "checked='checked'" : "";
                $tabindex = GFCommon::get_tabindex();
    
                $input = sprintf("<input name='input_%d' type='radio' value='%s' %s id='choice_%s' $tabindex $logic_event />", $field["id"], esc_attr($field_value), $checked, $id);
                $choices .= sprintf("<li class='gchoice_$id'><label for='choice_%s'>%s %s</label></li>", $id, $input, $choice["text"]);
    
                $count++;
            }
    
        }
    
        return $choices;
    }
    

    但是,如果您安装了 Gravity Forms Quiz Addon,并且您想将 THOSE 无线电输入移动到标签内,那么您需要此代码。唯一的区别是if($field["type"] != "radio")被if($field["type"] != "quiz")替换了。

    // Gravity Forms Quiz - move radio input inside label
    add_filter("gform_field_choices", "radio_input_inside_label", 10, 2);
    function radio_input_inside_label($choices, $field){
        if($field["type"] != "quiz") // <--- CHANGE HERE
            return $choices;
    
        $choices = "";
    
        if(is_array($field["choices"])){
            $choice_id = 0;
            $count = 1;
    
            $logic_event = !empty($field["conditionalLogicFields"]) ? "onclick='gf_apply_rules(" . $field["formId"] . "," . GFCommon::json_encode($field["conditionalLogicFields"]) . ");'" : "";
    
            foreach($field["choices"] as $choice){
                $id = $field["id"] . '_' . $choice_id++;
                $field_value = !empty($choice["value"]) || rgar($field, "enableChoiceValue") ? $choice["value"] : $choice["text"];
                $checked = rgar($choice,"isSelected") ? "checked='checked'" : "";
                $tabindex = GFCommon::get_tabindex();
    
                $input = sprintf("<input name='input_%d' type='radio' value='%s' %s id='choice_%s' $tabindex $logic_event />", $field["id"], esc_attr($field_value), $checked, $id);
                $choices .= sprintf("<li class='gchoice_$id'><label for='choice_%s'>%s %s</label></li>", $id, $input, $choice["text"]);
    
                $count++;
            }
    
        }
    
        return $choices;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-05
      相关资源
      最近更新 更多