根据(有点官方)this guide ,我也可以让单选按钮的标签为该单选按钮做出选择。他们说,
Always use labels for each checkbox and radio button. They associate text with a specific option and provide a larger clickable region.
不幸的是,我无法为我的表单获得此功能。这是我的代码:
<% form_for(@bet) do |f| %>
<%= f.label :bet_type %>
<%= f.radio_button :bet_type, "moneyline" %>
<%= f.label :bet_type_moneyline, "Moneyline" %>
<%= f.radio_button :bet_type, "spread" %>
<%= f.label :bet_type_spread, "Spread" %>
<% end %>
我也尝试过这个(因为这是示例使用 FormTagHelper 而不是 FormHelper 的方式):
<% form_for(@bet) do |f| %>
<%= radio_button_tag :bet_type, "moneyline" %>
<%= label_tag :bet_type_moneyline, "Moneyline" %>
<%= radio_button_tag :bet_type, "spread" %>
<%= label_tag :bet_type_spread, "Spread" %>
<% end %>
但是,我仍然无法让它工作。我使用的是 rails 2.3.5 和 ruby 1.8.7。
感谢阅读,或许还能提供帮助!
最佳答案
您可能找到了解决方案,但它可以帮助其他人。我正在使用 Rails 4+。
您可以使用 Rails' FormHelper's label method 使标签正确可点击在参数中使用“值”键。
label(:post, :privacy, "Public Post", value: "public")
# => <label for="post_privacy_public">Public Post</label>
对于你的代码,它应该是这样的
<%= f.radio_button :bet_type, "moneyline" %>
<%= f.label :bet_type, "Moneyline", value: "moneyline" %>
<%= f.radio_button :bet_type, "spread" %>
<%= f.label :bet_type, "Spread", value: "spread" %>
希望对您有所帮助:)
关于html - 单选按钮的标签也可以选择吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2032678/