我创建了一个表单select_tag
包含onchange
函数:
表单( advertisments / _form.html.erb ):
<div class="controls advertise-medium-select">
<%= select_tag 'advertisment[medium_id]', options_for_select(@mediums.collect { |mid| [mid.name, mid.id] }), :prompt => 'Select Media', onchange: 'get_media_categories()' %>
</div>
在我的 advertisments.js 文件中:如果我更改medium
选项,则控制台日志始终会打印select_tag
,但不会转到fetch_media_categories
方法
function get_media_categories(){
var medium = $('#advertisment_medium_id option:selected').val();
console.log(medium);
$.ajax({
url: '/advertisments/fetch_media_categories',
type: 'GET',
data: {medium_id: medium}
})
}
我的路线文件:
resources :advertisments do
collection do
get :fetch_media_categories, :fetch_media_pages
end
end
和 application.js 文件:
//= require jquery
//= require jquery.easing.1.3
//= require jquery_ujs
//= require twitter/bootstrap
//= require jquery.fancybox.pack
//= require jquery.fancybox-media
//= require prettify
//= require jquery.quicksand
//= require setting
//= require jquery.flexslider
//= require animate
//= require custom
//= require pricing_custom
//= require advertisments
//= require_tree .
目前,onchange功能正在使用chrome,但不适用于firefox(版本39.0)。有没有人有任何想法?
答案 0 :(得分:0)
在advertisment.js
中试试 $(function () {
$('#advertisment_medium_id').on('change', function() {
var medium = $('#advertisment_medium_id option:selected').val();
console.log(medium);
$.ajax({
url: '/advertisments/fetch_media_categories',
type: 'GET',
data: {medium_id: medium}
});
});
});