新手问题 请教下 radio_button 和 select 一起该怎么用

tt12580-github · 2017年03月01日 · 最后由 5swords 回复于 2017年03月01日 · 2382 次阅读

现在有个问题,有一个字段 city,用 radio_button 进行单选,选第一个把第一个的值传给 city,选第二个时 要从下拉框中选取并存到 city 中

<div class="container">
  <div class="col-lg-6 col-lg-offset-3">
    <%= simple_form_for @post do |f| %>
      <div class="col-lg-8">
        <%= f.check_box :colors, { :multiple => true }, "red" %>
        <%= f.label :colors, 'red', value: 'red' %>
        <div class="hide_red">
          <%= f.input :country, collection: Post::COUNTRIES.map{ |k, v| [k, k] }, as: :select %>
        </div>
        </div>
        <div class="col-lg-8">
        <%= f.check_box(:colors, { :multiple => true }, "black") %>
        <%= f.label :colors, 'black', value: 'black' %>
        <div class="hide_black">
          <div class="col-lg-8">
            <%= f.radio_button :city, "nb" %>
            <%= f.label :city, "nb", value: "nb" %>
          </div>
          <div class="col-lg-8">
            <%= f.radio_button :city, "#{@post.city}" %>
            <%= f.select :city, Post::CITIES - [Post::CITIES[0]], html: { id: "select_city" } %>
          </div>
        </div>
      </div>

      <div class="col-lg-12">
        <%= f.button :submit %>
      </div>
    <% end %>
  </div>
</div>

Rails 没法帮你写 JavaScript

不要因为用了 Rails 就忘了 HTML, JavaScript, CSS 这些基础的东西。

@huacnlee 用 js 也不知道咋做啊

很久没用过 simple_form 了,感觉用 tag 做更自由点。这个我会用 hidden_field_tag 保存值,用 radio_button_tag 和 select_tag 的 onclick 里写 js 改 hidden_field_tag 的值。

tt12580-github 关闭了讨论。 03月06日 10:17
需要 登录 后方可回复, 如果你还没有账号请 注册新账号