【问题标题】:Rails simple form problem - How to put hidden input field last?Rails 简单表单问题 - 如何将隐藏的输入字段放在最后?
【发布时间】:2011-08-05 12:33:23
【问题描述】:

如何更改简单表单生成的输入元素的顺序?

因为隐藏的复选框确实会阻止单击标签。

我的 simple_form 代码:​​

<label>
  <span>Company<b></b></span>
  <%= f.check_box :company_is_true %>
</label>

表单简单表单生成:

    <label>
        <span>Company<b>0</b></span>
       <input type="hidden" value="0" name="search[company_is_true]">
<input type="checkbox" value="1" name="search[company_is_true]" id="search_company_is_true">
      </label>

我想生成的,点击标签时有效:

<label>
    <span>Company<b>0</b></span>

<input type="checkbox" value="1" name="search[company_is_true]" id="search_company_is_true">
    <input type="hidden" value="0" name="search[company_is_true]">
  </label>

【问题讨论】:

  • 你不应该把隐藏字段放在复选框之后。如果您注意到复选框和输入字段具有相同的名称。隐藏字段的目的是,如果未选中复选框,则不会在表单中传递来自 chackbox 的输入,并且具有相同名称的元素,最新元素获取值。因此,如果您没有选中复选框,则从隐藏字段传递值 0,但如果选中它,则从复选框中传递值 1。
  • 如果无法移动隐藏的输入字段,是否有任何技巧可以将点击功能应用于标签
  • 关于 HTML 语义,您不需要在标签内包含 span 标签元素。
  • 请更具体地说明您的上一条评论。点击标签做什么?将隐藏字段移到哪里?
  • 用户应该能够点击标签来选中和取消选中复选框

标签: ruby-on-rails ruby ruby-on-rails-3 simple-form


【解决方案1】:

我不知道如何在cmets列表中添加评论,但响应:

用户应该能够单击标签以选中和取消选中复选框 - Rails 初学者 1 小时前

阻止用户点击该标签的不是复选框,而是缺少 for=""

<label>
  <span>Company<b></b></span>
  <%= f.check_box :company_is_true %>
</label>

要么使用 作为提到的 agmcleod,要么在标签中添加一个 for="":

<label for="search_company_is_true">
  <span>Company<b></b></span>
  <%= f.check_box :company_is_true %>
</label>

【讨论】:

    【解决方案2】:

    在传统的 html 表单中,它的结构应该是这样的:

    <label for="name">Name: </label>
    <input type="text" name="name" />
    

    for 属性将其连接到具有 name 属性或 id 属性的字段,该值具有您在 for 中使用的值。要在 Rails 中执行此操作,请使用以下命令:

    <%= f.label :company_is_true, 'Company' %>
    <%= f.check_box :company_is_true %>
    

    【讨论】:

      猜你喜欢
      • 2020-06-14
      • 1970-01-01
      • 1970-01-01
      • 2013-07-23
      • 1970-01-01
      • 2013-04-23
      • 2013-05-01
      • 2019-12-26
      • 1970-01-01
      相关资源
      最近更新 更多