[英]rails javascript not working
我正在使用Rails 4和ruby1.9.3:在Webrick上運行Rails應用程序時,javascript無法正常工作。 所以我做了一些故障排除以解決這個問題。
1)當我運行檢查元素時,我看到包含正確的文件(feast.js),所以我猜問題不在於資產管道。
我嘗試運行一個名為“ addy”的簡單函數,該函數應該發出警報,但提示我可以看到包含js文件(我在文檔的頭部以及正文部分的底部都包含了該文件) )當我按鏈接時沒有任何反應,好像js文件沒有效果:
feast.js文件:
// this is something i commented out
//$function()(
//$(".add_participants").on("click", function(evt) {
var searchWindow = window.open("http://users/search","","","");
//in the search method create a javascript with the
//search window object as
//"searchWindow.opener.location.reload" so to reload
//the opener page.
// location.reload(true) will get it again from cached
// but i think its not needed
// });
$function remove_fields(link){
$(link).prev().attr({value: 1});
$(link).parent.hide;
}
function addy(){
alert('hello');
}
和呈現的布局(僅js部分包含標記):
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>the amazing COOK BOOK <%= "| #{@username}" if @username %></title>
<meta name="author" content="elad bezalel">
<%= javascript_include_tag('application')%>
<%= stylesheet_link_tag('login') %>
</ head>
application.js文件:
// This is a manifest file that'll be compiled into application.js, which will include
all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts,
vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a
relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the
bottom of the
// the compiled file.
//
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK
LINE SHOULD
// GO AFTER THE REQUIRES BELOW.
//
//= require jquery
//= require jquery_ujs
//= require_tree .
//
檢查元素(在瀏覽器中生成的相關視圖中):
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>the amazing COOK BOOK </title>
<meta name="author" content="elad bezalel">
<script src="/assets/jquery.js?body=1"></script>
<script src="/assets/jquery_ujs.js?body=1"></script>
<script src="/assets/feast.js?body=1"></script>
<script src="/assets/feast_invt.js?body=1"></script>
<script src="/assets/friend_invt.js?body=1"></script>
<script src="/assets/messeges.js?body=1"></script>
<script src="/assets/participation.js?body=1"></script>
<script src="/assets/posts.js?body=1"></script>
<script src="/assets/public.js?body=1"></script>
<script src="/assets/replies.js?body=1"></script>
<script src="/assets/application.js?body=1"></script>
<link href="/assets/login.css?body=1" media="screen" rel="stylesheet">
</head>
和視圖:
<h1>let the feast begin!!</h1>
<p>please enter all feast deatails</p><br />
<%= form_for(:feast, :url=>{:action=>'create'}) do |f|%>
<%=f.label("feast time", :feast_time)%><br /><br />
<%= f.datetime_select(:feast_time, :order => [:day, :month ,:year]) %>
<br /><br />
<%=f.label("feast place", :feast_place)%><br /><br />
<%= f.text_field(:feast_place) %> <br /><br />
<%=submit_tag("start the feast")%>
<%end%> <!-- form end -->
<%= link_to("addy",'#', :onclick => "addy(); return true;") %>
<p>
</p>
我也嘗試過
link_to_function("addy","addy();")
但是javascript無法正常工作...
問題是什么????????????
從$function
刪除$
,這應該會破壞整個javascript文件
我會檢查JavaScript控制台中是否有錯誤。 另外,在javascript控制台中,您可以嘗試一下,例如,鍵入您的方法addy();
。
話雖這么說,您的JavaScript代碼看起來完全錯誤:您編寫了$function
,我認為您提到了function
。 因此,您的JavaScript解析將失敗,控制台中將記錄錯誤,並且解析甚至無法達到addy
的定義。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.