2011-09-27 41 views
0

我跟着一個Railscast by RyanB將jQuery date_picker添加到表單中,並且現在停留了幾天。根據該指令,只有三個變化:Rails 3不顯示jQuery日期選擇器

  1. 變化application.html.erb包括jQuery和jQuery的UI(參見下面我application.html.erb)
  2. 變化date_select場文本適應jQuery。我改變了我的_form.html.erb變化:

    <%= f.text_field:born_in%>

  3. 添加到application.js中下面的語句:

    $(function() { 
    $("#spec_born_in").datepicker(); 
        }); 
    

對我來說棘手的問題是我正在使用FullCalendar(一個JavaScript插件)。出於某種原因,如果我向applicaiton.js添加任何內容,FullCalendar將不會顯示。因此,我需要將函數定義放到一個名爲application_jquery_helper.js的文件中,並在加載FullCalendar的js文件後加載它。

我已經完成了所有這些,但在我的表單中,文本字段只是一個文本字段,並且jQuery datepicker不會顯示出來。

我不知道我做了什麼不正確的阻止jQuery datepicker出現。我能想到的唯一原因是我在application.html.erb中包含了js文件的順序,但我不知道如何解決它。

下面是我在application.html.erb相當大:

<head> 
<%= csrf_meta_tag %> 
<%= render 'layouts/stylesheets'%> 
<%= javascript_include_tag :defaults %> 
<!-- <%=stylesheet_link_tag'blueprint/screen',:media=>'screen'%> 
<%=stylesheet_link_tag'blueprint/print',:media=>'print'%> --> 
<%= stylesheet_link_tag 'formtastic', 'formtastic_changes', :cache => "base" %> 

<%= stylesheet_link_tag "http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/themes/redmond/jquery-ui.css", "application" %> 
<%= javascript_include_tag "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js", 
      "http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js", "application" %> 

<%= stylesheet_link_tag "fullcalendar.css" %> 
<%= stylesheet_link_tag "application.css" %> 
<%= javascript_include_tag "jquery.js" %> 
<%= javascript_include_tag "jquery.rest.js" %> 
<%= javascript_include_tag "rails.js" %> 
<%= javascript_include_tag "application.js" %> 
    <!-- these are needed for the calendar. --> 
<%= javascript_include_tag "jquery-ui-1.8.11.custom.min.js" %> 
<%= javascript_include_tag "fullcalendar.js" %> 
<%= javascript_include_tag "calendar.js" %> 
<%= javascript_include_tag "application_jquery_helper.js" %> 

意見或建議,非常感謝!

回答

0

我相信你試圖在DOM沒有完全加載時訪問一個元素。

要解決此問題:

$(document).ready(function(ev) { 
    $("#spec_born_in").datepicker(); 
}); 

你也可以看看jQuery's .live() functionality,如果你需要的「未來」的支持。

+0

謝謝Adam。就我而言,原型和jQuery之間的衝突導致了這個問題。 – AdamNYC

+0

啊,是的,有時候他們打得不好。 –