【问题标题】:Can the ActionView form input methods create <input> tags with inner html?ActionView 表单输入法可以创建带有内部 html 的 <input> 标签吗?
【发布时间】:2011-01-09 16:01:18
【问题描述】:

我正在生成这样的输入:

<%= form.radio_button("ans", ans.num.to_s) %>

生成的html是:

<input id="myform_ans_1" name="myform[ans]" type="radio" value="1" />

但我要找的是这个html:

<input id="myform_ans_1" name="myform[ans]" type="radio" value="1">Some Text</input>

是否有一个选项可以传递给 radio_button 方法以呈现所需的 html?

据我了解,您可以将可选的options hash 传递给radio_button,这只会将属性添加到 html 标记。

【问题讨论】:

  • 这是有效的标记吗?我什至不知道这会呈现什么......

标签: html ruby-on-rails actionview


【解决方案1】:

如果您的应用程序涉及很多表单,您绝对应该看看formtastic。您可以像这样定义表单:

<% semantic_form_for @article do |form| %>

  <% form.inputs :name => "New Article" do %>
    <%= form.input :title %>
    <%= form.input :body %>
  <% end %>

  <% form.buttons do %>
    <%= form.commit_button %>
  <% end %>

<% end %>

这将生成 html 表单所需的所有标记,包括字段集、图例、输入和标签。

【讨论】:

    【解决方案2】:

    你应该像这样添加带有for属性的&lt;label&gt;标签:

    <input id="myform_ans_1" ... /><label for="myform_ans_1">Foobar</label>
    

    不,没有参数。你只需要使用标签助手。

    【讨论】:

      【解决方案3】:
      <p><%= form.radio_button("ans", ans.num.to_s) %>
      
      <%= form.label("Some Text") %></p>
      

      【讨论】:

      • 这行得通,但我添加了&lt;%= form.label(:"ans_#{ans.num}", "Some Text") %&gt; 以使标签的 for 属性与输入字段的 id 相同。
      猜你喜欢
      • 1970-01-01
      • 2012-03-16
      • 2012-10-09
      • 2023-03-05
      • 2016-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-21
      相关资源
      最近更新 更多