复选框不添加选中属性

checboxes doesn't add checked attribute

在覆盖 Grav CMS 复选框时遇到一些问题。 问题是 checked 属性没有出现。为此,Grav 在 twig 模板中使用 {% if checked %}checked="checked"{% endif %}。但它不起作用。单击后不会添加任何内容。

它不适用于我的代码。

覆盖themes/child-theme/templates/forms/fields/checkboxes/checkboxes.html.twig

{% extends "forms/field.html.twig" %}

{% set originalValue = value %}
{% set value = (value is null ? field.default : value) %}
{% if field.use == 'keys' and field.default %}
    {% set value = field.default|merge(value) %}
{% endif %}

{% block global_attributes %}
    {{ parent() }}
    data-grav-keys="{{ field.use == 'keys' ? 'true' : 'false' }}"
    data-grav-field-name="{{ (scope ~ field.name)|fieldName }}"
{% endblock %}

{% block input %}
    {% for key, text in field.options %}

        {% set id = field.id|default(field.name)|hyphenize ~ '-' ~ key %}
        {% set name = field.use == 'keys' ? key : id %}
        {% set val = field.use == 'keys' ? '1' : key %}
        {% set checked = (field.use == 'keys' ? value[key] : key in value) %}
        {% set help = (key in field.help_options|keys ? field.help_options[key] : false) %}

        <div class="checkboxes {{ form_field_wrapper_classes }} {{ field.wrapper_classes }}">
            <input type="checkbox"
                   id="{{ id|e }}"
                   value="{{ val|e }}"
                   name="{{ (scope ~ field.name)|fieldName ~ '[' ~ name ~ ']' }}"
                   class="{{ form_field_checkbox_classes }} {{ field.classes }}"
                   {% if checked %}checked="checked"{% endif %}
                   {% if field.disabled or isDisabledToggleable %}disabled="disabled"{% endif %}
            >
            <label style="display: inline" for="{{ id|e }}">
                {% if help %}
                    <span class="hint--bottom" data-hint="{{ help|t|e('html_attr') }}">{{ text|t|e }}</span>
                {% else %}
                    {{ text|t|e }}
                {% endif %}
            </label>
        </div>
    {% endfor %}
{% endblock %}

md

中的复选框定义

        -
            name: planner_project_type_checkbox
            type: checkboxes
            label: false
            options:
                Text Option: 'Text Value'
                Text Option: 'Text Value'
                Text Option: 'Text Value'
            outerclasses: 'fields-group-controls onyx-checkboxes'
            classes: onyx-checkboxes

DOM个元素,点击后没有checked="checked"

quark theme 也不行。复选框定义:

---
form:
    name: myform
    fields:
        -
            name: myField
            type: checkboxes
            options:
              option1: Option 1
              option2: Option 2         
---

DOM个元素,点击后没有checked="checked"

"DOM个元素,点击后没有checked="checked"

这就是复选框的设计方式...参见 MDN docs about the checkbox

checked
A Boolean attribute indicating whether or not this checkbox is checked by default (when the page loads). It does not indicate whether this checkbox is currently checked: if the checkbox's state is changed, this content attribute does not reflect the change. (Only the HTMLInputElement's checked IDL attribute is updated.)

注意:“指示是否选中此复选框默认

另外,试试页面顶部的复选框示例。选中或取消选中复选框时,DOM 没有变化。