0

In my project we have inline javascript in a partial which is using some instance variables. When we are rerendering that partial after successful ajax call that script is not running.

Can you please help me that how I can solve that problem?

For e.g.

_partial.html.erb

 <div class="someclass"></div>
 <script>
    $(document).ready(function(){
       $(".someclass").hide();
    });
 </script>

The scrip tag is not running on replacing this partial from js.erb file.

4

3 回答 3

4

您的页面已加载。这ready件事很久以前就过去了,现在没人听了。

因此,如果您$(document).ready在 Ajax 中加载了脚本,则代码将永远不会被执行。

解决方案:

删除“就绪”事件并直接触发代码:

 <div class="someclass"></div>
 <script>
   $(".someclass").hide();
 </script>
于 2013-05-12T10:30:45.477 回答
2

在我看来,在 ajax 成功事件上手动更新 DOM 后未触发就绪事件,因此未执行回调函数。

于 2013-05-12T08:51:57.010 回答
1

最后,我发现我认为非常适合我的要求的答案是将 javascript 放入部分中并相应地在和 otherjs.erb中呈现它。html.erbjs.erb files

于 2013-05-14T05:24:59.700 回答