[英]Using Jquery Datepicker works on one page, not on another, in my rails app
I have a rails application. 我有一个Rails应用程序。 On two different html pages, I would like to use jquery's datepicker.
在两个不同的html页面上,我想使用jquery的datepicker。 The first time I use this, it works just fine:
我第一次使用它时,效果很好:
in my html: 在我的html中:
<input id="kid_dob" name="kid[dob]" type="text" />
in my javascript: 在我的javascript中:
(function() {
jQuery(function() {
return $('#kid_dob').datepicker({
changeMonth: true,
changeYear: true,
yearRange: '-16:c'
})({
dateFormat: 'yy-mm-dd'
});
});
}).call(this);
(This is actually written in CoffeeScript, the JavaScript that I show above has been processed by rails). (这实际上是用CoffeeScript编写的,上面显示的JavaScript已由rails处理)。
This works fine: the datepicker calendar does appear when I click on the kid_dob
text field. 这很好用:当我单击
kid_dob
文本字段时,日期选择器日历确实出现。
However, on another page I have the html 但是,在另一页上,我有html
<input id="reservable_history_start_date" name="reservable_history[start_date]" type="text" />
and the javascript (which is exactly the same except for kid_dob
being replaced by reservable_history_start_date
) 和javascript(除了
kid_dob
被reservable_history_start_date
替换之外, kid_dob
内容完全相同)
(function() {
jQuery(function() {
return $('#reservable_history_start_date').datepicker({
changeMonth: true,
changeYear: true,
yearRange: '-16:c'
})({
dateFormat: 'yy-mm-dd'
});
});
}).call(this);
And yet, in this second instance, the datepicker calendar does not appear. 但是,在第二种情况下,日期选择器日历未出现。 Also, when I use the options inspector, in the first working case, the scrollover text is
input#kid_dob.hasDatepicker
, but in the not working case, the scrollover text is simply input#reservable_history_start_date
(with no hasDatepicker
). 另外,当我使用选项检查器时,在第一个工作情况下,滚动文本是
input#kid_dob.hasDatepicker
,但在不工作情况下,滚动文本只是input#reservable_history_start_date
hasDatepicker
(没有hasDatepicker
)。
Also, here are the require
statements from my application.js
file 另外,这是我的
application.js
文件中的require
语句
//= require jquery
//= require jquery_ujs
//= require jquery.ui.datepicker
//= require bootstrap
//= require_tree .
What am I doing wrong?! 我究竟做错了什么?! Must be something different between the two html files, but I'm at a loss.
两个html文件之间必须有所不同,但是我很茫然。 Any hints are much appreciated.
任何提示都非常感谢。
This is all part of a rails 4.0.0 application, the output of ruby -v
is ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-linux] I am using ubuntu 10.04 Lucid Lynx. 这全部是Rails 4.0.0应用程序的一部分,
ruby -v
的输出是ruby 2.0.0p247(2013-06-27修订版41674)[x86_64-linux]我正在使用ubuntu 10.04 Lucid Lynx。
Thanks in advance! 提前致谢!
Maybe you can try adding below functions to your code,because the datepicker may not be loading. 也许您可以尝试在代码中添加以下功能,因为日期选择器可能未加载。
$(document).on('page:load')
$(document).ready();
I worked it out. 我解决了。 Turns out, the error console (ctrl+shift+j) is incredibly helpful.
事实证明,错误控制台(ctrl + shift + j)非常有用。 There was a null string being passed to
getElementById()
, from some code inside jquery-rails
. 从
jquery-rails
内的某些代码getElementById()
一个空字符串传递给getElementById()
。 By methodically removing every line in my application.js
file, until all I had was the \\\\= require jquery-rails
line left, I could get that error to go away. 通过有条不紊地删除
application.js
文件中的每一行,直到只剩下\\\\= require jquery-rails
行,我就可以消除该错误。 Then I slowly added one library at a time, until I found what was wrong (as I was sure that jquery-rails
couldn't have such a simple bug). 然后我一次慢慢地添加一个库,直到发现问题所在(因为我确信
jquery-rails
不会有这么简单的错误)。 Turns out that my version of jquery-rails
and bootstrap
had a clash - so I upgraded my version of bootstrap
, and the getElementByID()
problem was fixed, and now datepicker
can work in both instances. 原来,我的
jquery-rails
和bootstrap
发生了冲突-因此,我升级了bootstrap
版本,并修复了getElementByID()
问题,现在datepicker
在两种情况下都可以使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.