微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

感谢阅读,也许还有帮助!

解决方法

对单选按钮执行此操作的简单方法是将输入标记放在标签内,如下所示:
<label>
  <input />
  This is an input!
</label>

这是实现目标的有效方式.

在Rails中,标签助手可以接受一个块,所以你可以这样做:

<%= f.label :moneyline do %>
  <%= f.radio_button :bet_type,"moneyline" %>
  "Moneyline"
<% end %>

原文地址:https://www.jb51.cc/html/226275.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐