【发布时间】: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