也有单选按钮的标签可供选择吗? [英] Have radio button's label make selection too?

查看:31
本文介绍了也有单选按钮的标签可供选择吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据(有些官方的)本指南,我可以制作收音机按钮的标签也可以选择该单选按钮.他们说,

According to (somewhat official) this guide, I can make a radio button's label make the selection for that radio button too. They say,

始终为每个复选框和单选按钮使用标签.他们将文本与特定选项关联,并提供更大的可点击区域.

很遗憾,我无法为我的表格获取此功能.这是我的代码:

Unfortunately, I have not been able to get this functionality for my form. Here is my code:

<% 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的方式):

I've also tried this (because this is the example's way using FormTagHelper instead of 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.

But, I still cannot get it to work. I'm using rails 2.3.5 and ruby 1.8.7.

感谢阅读,也许有帮助!

Thanks for reading, and maybe helping!

推荐答案

对单选按钮执行此操作的一种简单方法是将输入标签放置在标签内,如下所示:

An easy way to do this for radio buttons is to place the input tag inside the label, like so:

<label>
  <input />
  This is an input!
</label>

这是达成目标的有效方法.

This is a valid way of accomplishing your goal.

在Rails中, label 帮助器可以接受一个块,因此您可以执行以下操作:

In Rails the label helper can accept a block, so you could do:

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

这篇关于也有单选按钮的标签可供选择吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆