【问题标题】:Text input inside a radio button?单选按钮内的文本输入?
【发布时间】:2018-01-04 19:21:18
【问题描述】:

WordPress 的 Gravity Forms 的单选按钮集有一个选项,称为“启用“其他”选项”。此选项添加嵌套在选项内的文本输入。

GF 的单选按钮一开始是个问题,因为它们没有字段集,但忽略这一点,这种其他选择会导致键盘可访问性问题。当您选择单选按钮组时,您很好,但是一旦您再次选择选项卡,焦点就会转到“其他”文本输入,您的单选按钮选择会更改为该选项,并且无法更改单选按钮返回到任何其他值。

这些怪物之一的代码是:

<li id='field_4_4' class='gfield gfield_contains_required field_sublabel_below field_description_below gfield_visibility_visible' >
    <label class='gfield_label'  >
        Overall label that applies to all of the options
    </label>
    <div class='ginput_container ginput_container_radio'>
        <ul class='gfield_radio' id='input_4_4'>
            <li class='gchoice_4_4_0'>
                <input name='input_4' type='radio' value='First option'  id='choice_4_4_0' tabindex='14'    />
                <label for='choice_4_4_0' id='label_4_4_0'>
                First option</label>
            </li>
            <li class='gchoice_4_4_1'>
                <input name='input_4' type='radio' value='Second option'  id='choice_4_4_1' tabindex='15'    />
                <label for='choice_4_4_1' id='label_4_4_1'>
                Second option</label>
            </li>
            <li class='gchoice_4_4_2'>
                <input name='input_4' type='radio' value='Third option'  id='choice_4_4_2' tabindex='16'    />
                <label for='choice_4_4_2' id='label_4_4_2'>
                Third option</label>
            </li>
            <li class='gchoice_4_4_3'>
                <input name='input_4' type='radio' value='gf_other_choice'  id='choice_4_4_3' tabindex='17'   onfocus="jQuery(this).next('input').focus();" />
                <input id='input_4_4_other' name='input_4_other' type='text' value='Other' aria-label='Other' onfocus='jQuery(this).prev("input")[0].click(); if(jQuery(this).val() == "Other") { jQuery(this).val(""); }' onblur='if(jQuery(this).val().replace(" ", "") == "") { jQuery(this).val("Other"); }' tabindex='17'   />
            </li>
        </ul>
    </div>
</li>

有没有办法让这个单选按钮组可以访问?我知道我可以取消选中“启用“其他”选项”并将“其他”创建为普通单选按钮,然后添加一个单独的文本输入作为条件字段来解决这个问题。问题是它在一个有多个编辑器的网站上添加和编辑表单,他们是非技术用户,有时可能会自己使用这个选项 - 所以我想找到一种方法让它工作,如果可能的话。

【问题讨论】:

  • 现在我只想知道 HTML 标记的哪些部分需要更改以使整个键盘都可以访问。我会弄清楚如何欺负重力形式将其作为一项单独的任务吐出来。

标签: wordpress accessibility


【解决方案1】:

这需要 3 个步骤: 1.为单选按钮添加标签 2.删除标签索引 3.添加CSS

这是更新后的 HTML:

<li class='gchoice_4_4_3'>
    <!-- no more tabindex -->
    <input name='input_4' type='radio' value='gf_other_choice'  id='choice_4_4_3' onfocus="jQuery(this).next('input').focus();" />
    <!-- new label -->
    <label for='choice_4_4_3'>Other</label>
    <!-- no more tabindex -->
<input id='input_4_4_other' name='input_4_other' type='text' value='Other' aria-label='Other' onfocus='jQuery(this).prev("input")[0].click(); if(jQuery(this).val() == "Other") { jQuery(this).val(""); }' onblur='if(jQuery(this).val().replace(" ", "") == "") { jQuery(this).val("Other"); }'  />
</li>

还有 CSS,默认隐藏文本输入,但在选择“其他”单选按钮时显示:

.gfield_radio li input[type="text"] { display:none; }
.gfield_radio li input:checked + label + input[type="text"] { display:block; }

CSS 进入我的自定义主题,更改 HTML 的过滤器是:

add_filter('gform_tabindex', function($tabindex, $form) {
    return false;
}, 10, 2);

如果 Gravity Forms 能与时俱进,并将所有内容设置在 &lt;fieldset&gt; 中,那就太好了,但至少有了这些更改,“其他”单选按钮现在可以通过键盘访问了。

【讨论】:

  • 我一直在寻找这种模式的可访问版本。但我注意到onFocus 事件在您重新排序 HTML 时找不到下一个输入。您是否碰巧做了其他可以分享的更改?
  • 我开始使用一个名为“WCAG 2.0 form fields for Gravity Forms”的插件,对一些人有帮助。
  • 感谢您的回复!谢谢,我去看看。
  • PS- 我可能会尝试使脚本和类更通用一些(因为我没有使用重力形式)但这里是 jsFiddle 工作。
猜你喜欢
  • 1970-01-01
  • 2020-06-25
  • 1970-01-01
  • 2020-12-08
  • 1970-01-01
  • 2011-02-25
  • 2012-03-02
  • 1970-01-01
  • 2012-01-27
相关资源
最近更新 更多