【问题标题】:Rails bootstrap-datepicker-rails not workingRails bootstrap-datepicker-rails 不工作
【发布时间】:2014-10-16 12:13:06
【问题描述】:

我有一个使用 twitter-bootstrap-rails 2.2.6 和 bootstrap-datepicker-rails 1.3.0.2 的 Rails 3.2.19 应用程序。

我试图在表单中获取一个 text_field 以显示日期选择器,但是当我单击或关注该字段时没有任何反应。

当我检查元素时,我看到以下内容:

<input data-behaviour="datepicker" id="project_due_date" name="project[due_date]" size="30" type="text">

检查控制台时,我没有收到任何 JS/Coffee 错误。

这是我的代码:

projects.js.coffee

 $(document).on "focus", "[data-behaviour~='datepicker']", (e) ->
 - $(this).datepicker
 - format: "yyyy-mm-dd"
 - weekStart: 1
 - autoclose: true

projects/_form.html.erb

<%= form_for(@project, :html => { :class => "well"}) do |f|  %>
 <% if @project.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@project.errors.count, "error") %> prohibited this contact from being created:</h2>

      <ul>
      <% @project.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <%= f.label :project_name %>
  <%= f.text_field :project_name, placeholder: "Build Spreadsheet", required: true %>
  <%= f.label :project_manager %>
  <%= f.collection_select(:user_id, User.all, :id, :full_name)%>
  <%= f.label :due_date %>
  <%= f.text_field :due_date, 'data-behaviour' => 'datepicker' %>
  </br>
<%= f.button :submit, :class => 'btn btn-info' %>
  <% end %>

我也尝试过根据 id 调用它,但没有运气。我在 Chrome、Firefox 或 Safari 中看到了相同的行为。

非常感谢任何帮助。

【问题讨论】:

    标签: ruby-on-rails-3 twitter-bootstrap coffeescript bootstrap-datepicker


    【解决方案1】:

    看来我的 CoffeeScript 是错误的。我将其更改为以下内容:

    $ ->
      $('[data-behaviour~=datepicker]').datepicker(format: "yyyy-mm-dd", autoclose: true)
    

    现在我可以选择字段并且日期选择器出现了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-18
      • 2016-04-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多