在f.label里面嵌套输入(rails form generation) [英] Nest input inside f.label ( rails form generation )

查看:127
本文介绍了在f.label里面嵌套输入(rails form generation)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用f.label方法创建我的表单元素标签,但是 - 我想要将表单元素嵌套在标签内。这可能吗?

- 从W3C -


要将标签与另一个控件关联隐含地,控制元素必须在LABEL元素的内容中。在这种情况下,LABEL可能只包含一个控制元素。标签本身可以位于关联控件之前或之后。



在此示例中,我们隐式地将两个标签与两个文本输入控件相关联:




 < form action =...method =post> 
< p>
< label>
名字
< input type =textname =firstname/>
< / label>
< label>
< input type =textname =lastname/>
姓氏
< / label>
< / p>
< / form>


解决方案

您可以将表单助手嵌套到标签中一个块。这是一个使用HAML的例子,但它也适用于ERB。

  = form_for your_resource do | f | 
= f.label:first_name do
= f.text_field:first_name


I want to use the f.label method to create my form element labels, however - i want to have the form element nested inside the label. Is this possible?

-- From W3C --

To associate a label with another control implicitly, the control element must be within the contents of the LABEL element. In this case, the LABEL may only contain one control element. The label itself may be positioned before or after the associated control.

In this example, we implicitly associate two labels with two text input controls:

<form action="..." method="post">
 <p>
  <label>
     First Name
     <input type="text" name="firstname" />
  </label>
  <label>
     <input type="text" name="lastname" />
     Last Name
  </label>
  </p>
</form>

解决方案

You can nest your form helper inside of a label using a block. Here is an example using HAML, but it also works with ERB.

= form_for your_resource do |f|
  = f.label :first_name do
    = f.text_field :first_name

这篇关于在f.label里面嵌套输入(rails form generation)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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