我有一個有很多Javascript的表單(特別是jQuery)。當我通過手動輸入URL來導航到頁面時,或者從我的登錄頁面重定向到頁面時,所有JavaScript元素都加載得很好。但是,當我從索引中的鏈接導航到頁面時,除非刷新,否則JavaScript都不加載。 (我不知道這是否是排除索引的唯一問題;索引只是包含表單的唯一鏈接)。從索引鏈接加載頁面時,Javascript無法加載
我已將我的所有JavaScript合併爲init.js
,其中require
d在我的application.js
中。這是因爲如下:
jQuery(document).ready(function($) {
// scolling accelleration
$(".anchor_link").click(function(event) {
event.preventDefault();
$('html,body').animate({ scrollTop:$(this.hash).offset().top } , 1000);
});
$('#save').click(function(){
$('#save_alert').animate({"bottom":"100px"}, "slow").delay(800).animate({"bottom":"50px"}, "slow");
});
//styling radio buttons
function radStyle(entity){
$(entity).find('input[type="radio"]').css("display", "none");
$(entity).find('input[type="radio"]').parent().prepend('<span class="radiospan fa-stack">')
$(entity).find("span.radiospan").append('<i class="fa fa-circle fa-stack-2x fa-inverse">')
$(entity).find('input[type="radio"]:checked').siblings("span.radiospan").append('<i class="fa fa-circle fa-stack-1x">');
}
//calling radio buttons on document ready
radStyle(document);
//adding interactive radio button functionality (delegate to provide functionality to dynamically generated fields)
$('div.form-right').delegate('input[type="radio"]', 'click', function(){
if($(this).is(':checked')){
$(this).siblings("span.radiospan").append('<i class="fa fa-circle fa-stack-1x">');
$(this).closest('span.radio').siblings('span.radio').find('i.fa.fa-circle.fa-stack-1x').remove();
}
});
//select menus on initial load
$('select').customSelect();
//styling select menus and radio buttons upon dynamic content generation
var nests = [ "dynamic-officer", "dynamic-director", "dynamic-contractor-person", "dynamic-contractor-org", "dynamic-ip", "dynamic-shareholder"];
nests.forEach(function(nest){
$("#"+nest).on("cocoon:after-insert", function(e, added_item){
added_item.find('select').customSelect();
radStyle(added_item);
});
});
});
多幾個音符
- 添加到我的困惑,螢火蟲似乎承認我init.js的存在,不扔在錯誤控制檯什麼。
- 我注意到,如果我將我的
init.js
中的所有內容都複製到嵌入的<script>
部分中,那麼它將加載得很好。
無論如何,這完全是可能的,這只是軌道如何工作,我需要在我的視圖中使用腳本標記(我想我會讀它不是,但誰知道)。任何有關這件事的指針肯定會受到讚賞;在此先感謝任何直接告訴我的人。
如果它有什麼做的錯誤,我index.html.erb
是
<div class="container-fluid">
<h1>Incorporation#index</h1>
<table class="table table-hover">
<% @incorporations.each do |incorporation| %>
<tr>
<td class="name">
<% begin %>
<%= link_to incorporation.company.names.first.name_string, edit_incorporation_path(incorporation) %>
<% rescue %>
<%= link_to "Untitled Company", edit_incorporation_path(incorporation) %>
<% end %>
</td>
<td class="email"><%= link_to incorporation.user.email, incorporation %></td>
<td class="created_at"><%= link_to incorporation.created_at, edit_incorporation_path(incorporation) %></td>
<td class="generate"><%= button_to "Generate Documents", incorpgenerate_incorporation_path(incorporation), :remote => true, :method => :post %></td>
</tr>
<% end %>
</table>
</div>