gpt4 book ai didi

html - 表单验证失败后表单上的单选按钮无法正确呈现 - bootstrap

转载 作者:搜寻专家 更新时间:2023-10-31 08:05:59 24 4
gpt4 key购买 nike

带有 Bootstrap 3 的 Rails 4 应用

我有一个包含单选按钮输入的表单:

<strong> Radio button options on form </strong><br>
<div class="form-group">
&nbsp<%= f.radio_button :category, 'A' %> &nbsp <%= f.label "A", "A" %><br />
&nbsp<%= f.radio_button :category, 'B' %> &nbsp <%= f.label "B", "B" %><br />
&nbsp<%= f.radio_button :category, 'C' %> &nbsp <%= f.label "C", "C" %><br />
</div>

第一次加载表单时,一切看起来都是正确的:

enter image description here

我的问题是,如果表单由于验证错误(表单上任何地方缺少必需的输入等)而失败,当页面重新呈现时,单选按钮和标签显示在不同的两行:

enter image description here

我该如何解决这个问题?

已更新为每个生成的 HTML 是:原文(正确):

    <strong> Radio button options on form </strong><br>
<div class="form-group">
&nbsp<input id="listing_category_1" name="listing[category]" type="radio" value="1" /> &nbsp <label for="listing_1">A</label><br />
&nbsp<input id="listing_category_2" name="listing[category]" type="radio" value="2" /> &nbsp <label for="listing_2">B</label><br />
&nbsp<input id="listing_category_3" name="listing[category]" type="radio" value="3" /> &nbsp <label for="listing_3">C</label><br />
</div>

对于重新渲染(不正确):

    <strong> Radio button options on form </strong><br>
<div class="form-group">
&nbsp<div class="field_with_errors"><input id="listing_category_1" name="listing[category]" type="radio" value="1" /></div> &nbsp <label for="listing_1">A</label><br />
&nbsp<div class="field_with_errors"><input id="listing_category_2" name="listing[category]" type="radio" value="2" /></div> &nbsp <label for="listing_2">B</label><br />
&nbsp<div class="field_with_errors"><input id="listing_category_3" name="listing[category]" type="radio" value="3" /></div> &nbsp <label for="listing_3">C</label><br />
</div>

最佳答案

这是因为 Rails 将有错误的字段包装到 div 中:

<div class="field_with_errors">
<input name="foo" type="radio" value="1" />
</div>

一种解决方法是自定义这个 css 类,例如让 div 显示属性 inline 而不是 block:

.field_with_errors { display: inline; }

另一种选择,修改 Rails 设置以更改显示错误时的默认行为:

config.action_view.field_error_proc = Proc.new { |html_tag, instance| "<span class='field_with_errors'>#{html_tag}</span>".html_safe }

关于html - 表单验证失败后表单上的单选按钮无法正确呈现 - bootstrap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22209855/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com