简体   繁体   中英

Rails - jQuery in form partial works on edit but not on new

I have a partial which is rendered using identical code between the new and the edit controller actions, containing a form for the associated model and nested resources.

As a troubleshooting step for working out why the jQuery included in this partial was working on the edit form and not the new form, I've removed all existing jQuery and replaced it with this:

$("input").click(function() { alert("test!"); });

This works as expected when loading the edit form, all input fields/buttons/dropdowns display the alert box when clicked. However, when the new form loads, this does not apply.

Any suggestions as to how I can troubleshoot this?

It sounds like you might have a javascript error on the new form which is breaking javascript and stopping it from executing further. Can you use your browser inspector to see if there are any javascript errors on the page?

This was all caused by an issue with a plugin I was using to help with displaying some of the content; I passed in the string of the div id as an argument to it, and instead of applying its methods directly to the div in the DOM, it duplicated it all further down the page, meaning the id's were being recreated and in addition, meant the jQuery executed on page load didn't apply to the new objects.

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