【问题标题】:Ruby on Rails disabled field in form select not working表单选择中的 Ruby on Rails 禁用字段不起作用
【发布时间】:2015-02-08 00:41:05
【问题描述】:
<%= f.select :minutes, options_for_select([['1 hour', 60], ['1 hour and a half', 90], ['2 hours', 120]][0..(@minutes_to_schedule/30-2)], 60), {}, {disabled: true} %>

我检查了表单助手 API,但尽管遵循了我的选项,但我的选项仍然没有被禁用,有什么我忽略的吗?我看了看其他堆积的帖子,似乎没有做错什么

   <%= form_for(@session_date, html: {class: 'no_custom'}) do |f| %>

      <%= f.hidden_field :student_id, value: @student.id %>
      <%= hidden_field_tag 'from', schedule_student_path(id: @student.id) %>          
      <div id="appointment_display"></div>         
      <input id="appointment_at" type="datetime-local"  name="appointment[at]" step="900" min= "some_time" max="max_time" required="required" placeholder="Use Chrome!" style="width: auto; display: none;"/>
      <br/>
      <div id="appointment_location_display"></div> 
      <br/>
      <%= f.select :minutes, options_for_select(@appointment_time_options, 60) %>
      <br/> 
      <%= f.collection_select(:location_id, Location.order('city, state, address'), :id, :name, {}, {class: 'dont_display'}) %>
      <%= f.submit 'Create Appointment' %>
      <br/>

    <% end %>    

【问题讨论】:

    标签: ruby forms select ruby-on-rails-4 disabled-input


    【解决方案1】:

    disabled: true 应该是第四个参数

    检查下面的定义

    options_from_collection_for_select(collection, value_method, text_method, selected = nil)
    


    所以,正确的做法...

    <%= f.select : minutes, options_for_select( ([['1 hour', 60], ['1 hour and a half', 90], ['2 hours', 120]]), {}, {}, { disabled: true } )%>
    

    【讨论】:

      【解决方案2】:

      这看起来是正确的。

      当我复制/粘贴这个时:

      <%= f.select : minutes, options_for_select([['1 hour', 60], ['1 hour and a half', 90], ['2 hours', 120]]), {}, { disabled: true }%>
      

      进入 Rails 应用程序,生成正确的表单元素:

      <select disabled="disabled" id="post_minutes" name="post[minutes]">
         <option value="60">1 hour</option>
         <option value="90">1 hour and a half</option>
         <option value="120">2 hours</option>
      </select>
      

      并且该元素被禁用,就像它应该的那样。有没有 JS 操作这个字段?您介意提供您的完整表格以便我为您提供帮助吗?

      【讨论】:

      • 我编辑了我的帖子以包含我正在使用的表格。我正在使用 jQuery 使用 ID 操作表单中的任何内容
      猜你喜欢
      • 2010-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-01
      • 1970-01-01
      • 2011-05-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多