jQuery not working on Rails 3.2.8

后端 未结 4 1692
一向
一向 2021-01-19 22:07

I\'m having an issue with jQuery on Rails 3.2.8. I\'ve installed the jquery-rails gem and my application.js has the following:

//= require jquery
//= require         


        
4条回答
  •  时光说笑
    2021-01-19 22:39

    Make sure you're including the javascript include tag before the yield in your application.html.erb. If you're using Bootstrap, I think the default application.rb loads JS after the yield, which makes it necessary to wait until jQuery is loaded before running it in your views.

    If that's the case and you don't want to relocate the include tag higher on the page, you can do this:

    runScript();
    function runScript() {
        if( window.$ ) { // Make sure jquery is loaded
             // your jquery code here
        } else {
            window.setTimeout(runScript, 100); // If not, wait 100 milliseconds and try again.
        }
    }
    

提交回复
热议问题