onchange函数ajax调用不能在firefox中工作

时间:2015-08-29 09:34:33

标签: javascript jquery ruby-on-rails ajax

我创建了一个表单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)。有没有人有任何想法?

1 个答案:

答案 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}
      });
  });

});