简体   繁体   中英

jquery $(document).ready not being called after submit

In a Ruby on Rails 4 application I have a form which uses jQuery DatePicker (from the jquery-ui-rails Gem.

So I need to call

$('.datepicker').datepicker();

everytime my form is rendered.

I started with this (in my controller specific .js.coffee file.)

$ -> 
    $('.datepicker').datepicker

It went fine until I got the Turbolinks problem and had to change to:

ready = ->
    $('.datepicker').datepicker();

$(document).ready = ready
$(document).on 'page:change', ready

But I still have problems after hitting the submit button and there is one (or more) errors, so the form is re-rendered but none of the handlers ( document.ready and document.on page:change ) are called.

I've put this code in application.js

$(function(){
    console.log("application.js -> ready")
})

and I can see in the console that this is being called, but the functions defined in the controller specific .js file are not.

All the files seems to be loaded correctly (they have a <script> line in the <header> section; and appear in Chrome's Sources tab.

I'm not clear on the syntax above, but from a javascript/jquery perspective:

$(document).ready = ready

This doesn't register a function to call on the ready event, it clobbers the ready function that you are supposed to invoke to register a listener.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM