简体   繁体   English

Rails-表单中的jQuery部分可在编辑中使用,但不适用于新

[英]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. 我有一个部分,它在new和edit控制器动作之间使用相同的代码呈现,其中包含关联模型和嵌套资源的表单。

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: 作为解决此部分中的jQuery为什么在编辑表单而不是新表单上工作的原因的故障排除步骤,我删除了所有现有的jQuery并替换为:

$("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. 听起来您可能在新表单上遇到了javascript错误,这会破坏javascript并阻止其进一步执行。 Can you use your browser inspector to see if there are any javascript errors on the page? 您可以使用浏览器检查器查看页面上是否存在任何JavaScript错误?

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. 我传入了div id的字符串作为参数,而不是直接将其方法应用于DOM中的div,而是将其全部复制到了页面的下方,这意味着该id被重新创建,此外,这意味着页面加载时执行的jQuery不适用于新对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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