【问题标题】:How to align input and label from collection_check_boxes?如何对齐collection_check_boxes的输入和标签?
【发布时间】:2014-07-29 08:15:15
【问题描述】:

我正在使用collection_check_boxes,但在对齐复选框和文本时遇到问题。这是我的代码:

<div class="col-md-4">
  <%= f.collection_check_boxes :dog_ids, Dog.all, :id, :name %>
</div>

表格显示如下:

[checkbox1]
  text1

[checkbox2]
  text2

[checkbox3]
  text3

我正在尝试对齐输入和标签,但没有成功。我已经看到了这些问题,但它对我不起作用:Align checkboxes for f.collection_check_boxes with Simple_Form

我想完成这个:

[checkbox1] text1

[checkbox2] text2

[checkbox3] text3

感谢您的帮助!

【问题讨论】:

  • [checkbox1]
    text1
  • @Octopus-Paul,谢谢,但是如何将其应用于 collection_check_boxes?

标签: css ruby-on-rails twitter-bootstrap checkbox ruby-on-rails-4


【解决方案1】:

collection_check_boxes的定义:

collection_check_boxes(object, method, collection, value_method, text_method, options = {}, html_options = {}, &block)

最后一个参数允许你做这样的事情:(这正是你想要使用collection_check_boxes)

<%= f.collection_check_boxes(:dog_ids, Dog.all, :id, :name) do |b| %>
  <div class="row">
    <%= b.label(class: "check_box") do %>
      <div class="col-xs-4">
        <%= b.check_box(class: "check_box") %>
      </div>

      <div class="col-xs-8">
        <%= b.object.name %>
      </div>       
    <% end %>
  </div>
<% end %>

阅读更多关于collection_check_boxes

还有另一种方法:为您的复选框输入和 css 标签设置样式。

为了更好的 css 特异性,我将添加一个名为“checkbox-list”的新类:

<div class="col-md-4 checkbox-list">
  <%= f.collection_check_boxes :dog_ids, Dog.all, :id, :name %>
</div>

.checkbox-list input[type="checkbox"] {
  display: inline-block;
  width: 10%;
}

.checkbox-list input[type="checkbox"] + label {
  display: inline-block;
  margin-left: 10%;
  width: 80%;
}

【讨论】:

  • 谢谢!它对我来说看起来很棒。唯一的问题是,我不知道为什么会这样说:unexpected ',', expecting ')' 这部分::dog_ids, Dog.all
  • 请立即尝试。我做了一个小改动
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-19
  • 2014-09-04
  • 1970-01-01
  • 1970-01-01
  • 2018-10-09
  • 2015-09-17
  • 2015-05-28
相关资源
最近更新 更多