除了 $.getScript("/comments.js
部分) 之外,一切似乎都工作正常。我尝试修复它,但没有成功。
我的 application.js
文件包含:
$(function() {
setTimeout(test, 1000);
});
function test () {
$.getScript("/comments.js")
setTimeout(test, 1000);
}
$.getScript("/comments.js", function(){
updateComments();
});
以及 View /评论中的我的index.js.erb
function updateComments(){
alert("testing");
}
我的评论 Controller
def index
# ...
end
布局/application.html.erb
<%= stylesheet_link_tag "application" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
我只是想看看是否可以让它工作,但它什么也没做。
最佳答案
如果您尝试使用正在使用 getScript
加载的脚本中的任何方法,请等待脚本加载。使用其回调方法,一旦脚本加载到页面上就会调用该方法。试试这个。
$.getScript("/comments.js", function(){
//Write your code here
updateComments();
});
还要确保js文件的路径正确。
关于javascript - $.getScript 不工作 Rails 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9183127/