[英]JQuery work but only when i reload page
我试图做红宝石动态选择框。 jQuery看起来像这样:
jQuery ->
$('#person_state_id').parent().hide()
states = $('#person_state_id').html()
$('#person_country_id').change ->
country = $('#person_country_id :selected').text()
escaped_country = country.replace(/([ #;&,.+*~\':"!^$[\]()=>|\/@])/g, '\\$1')
options = $(states).filter("optgroup[label='#{escaped_country}']").html()
if options
$('#person_state_id').html(options)
$('#person_state_id').parent().show()
else
$('#person_state_id').empty()
$('#person_state_id').parent().hide()
并且鉴于:
<%= f.grouped_collection_select :state_id, Country.order(:name), :states, :name, :id, :name, include_blank: true %>
它有效,但是...当我转到example.com/person/new上的页面时-它不起作用。 我必须重新加载页面-然后一切正常...为什么它在第一次访问时不加载JQuery?
在$(document).ready()
内编写函数,以在列表框中加载项目。
此功能将在页面加载期间加载您的功能
$(document).ready(function() {
//your function;
});
正如伊恩所说,
在Rails 4.0中,只需将其添加到您的Gemfile中:
宝石'jquery-turbolinks'
并将其保存到您的Assets / javascripts / application.js文件中:
// =需要jquery.turbolinks
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.