html - 单选按钮的标签也可以选择吗?

标签 html ruby-on-rails ruby web-applications

根据(有点官方)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/

相关文章:

jquery - SVG 动画完成后如何加载另一个 HTML 页面?

Javascript 和 jQuery 在同一个 HTML 文件中

ruby-on-rails - 使用 Rack::Deflater 时,rails 中的 HTTP 流不起作用

ruby-on-rails - 使用Minitest,需要 'minitest_helper'加载错误

ruby-on-rails - 为什么我的 Rails 应用程序无法部署到 AWS Elastic Beanstalk?

html - 为什么他们不赞成使用预格式化文本的宽度属性?

javascript - 使用 CSS 使图像变暗

ruby-on-rails - Rails 中的 HTML 实体 link_to

java - 在 Ruby 中自动进行类级初始化

ruby - 如何在主绑定(bind)中运行 eval