简体   繁体   English

在控制台中执行模态对话框加载jquery,但不从application.js加载

[英]Modal-dialog loading jquery executing in console but not loading from the application.js

I have an app that is using twitter-bootstrap and I am trying to implement a button that would launch a modal-box loading users/new path. 我有一个使用twitter-bootstrap的应用程序,并且我试图实现一个按钮,该按钮将启动加载用户/新路径的模式框。 I am able to do this but only if I input the javascript portion of the code directly into the console. 我能够执行此操作,但前提是我直接将代码的javascript部分输入到控制台中。 Putting into the application.js does nothing. 放入application.js不会执行任何操作。

This is how it is set up in the view: 这是在视图中设置的方式:

<a class="btn" id="modal" data-toggle="modal" href="#userModal" >New Reserver</a>

<div class="modal hide" id="userModal">
</div>

If I press the button without executing any javascript an empty modal box loads just as expected. 如果我在不执行任何JavaScript的情况下按下按钮,则空模式框将按预期加载。 However if I run the following code in the javascript console in my browser while the modal box is open or before clicking on the button everything gets rendered properly. 但是,如果在模式框处于打开状态时或在单击按钮之前在浏览器的JavaScript控制台中运行以下代码,则所有内容都会正确呈现。 Inside of my users/new is a form partial that also gets rendered and functions properly. 在我的用户/新用户内部有一个表单部分,它也可以呈现并正常运行。 Why is it not executing properly inside of my application.js? 为什么在我的application.js内部无法正确执行?

$('#userModal').load('users/new #content');

Turns out the code was executing before the DOM was loading properly. 原来代码是在DOM正确加载之前执行的。 I was sure I had checked for that earlier but apparently my syntax was wrong. 我确定我之前已经检查过,但是显然我的语法是错误的。 This fixes it 这修复了它

$().ready(function() {
  $('#userModal').load('users/new #content');
});

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

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