Ajax发布后执行document.ready [英] Execute document.ready after ajax post

查看:82
本文介绍了Ajax发布后执行document.ready的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个custom.js文件,其中有几个单击并绑定了其他方法的元素.整个文件封装在document.ready()中,一切正常.但是,当我执行AJAX发布时,显然不会为当前页面再次触发document.ready().无论如何,我可以再次触发document.ready()还是我需要让命名函数中的所有内容都从我的create.js.erb中调用它们?

I have a custom.js file in which I have several elements that have click and other methods bound to them. The entire file is encapsulated in document.ready() and everything works. However when I do an AJAX post obviously document.ready() is never fired again for the current page. Is there anyway I can get document.ready() to fire again or do I need to have everything in named functions call them form my create.js.erb?

推荐答案

您总是可以将所有内容放在一个函数(名为loadfunction之类)中,并在文档加载时以及在ajax加载时再次调用该函数.尽管这是一个骇人听闻的解决方案,但它应该可以很好地工作.

You could always just put everything in one function (named loadfunction or something) and call that function when the document loads, and again when the ajax is loaded. Though it is a hacked together solution, it should work well enough.

因此,请取下$(document).onready(function () {及其末尾括号}之间的所有内容 并将其放在以}结尾的function OnloadFunction () {中. 然后放入$document.onready(OnloadFunction);

So then take everything between $(document).onready(function () { and its end bracket } And put it in function OnloadFunction () { ending with }. Then put $document.onready(OnloadFunction);

示例: 你有

$(document).ready(function () {alert("test");});

它将变成:

function OnloadFunction ()
{
    alert("test");
}
$(document).ready(OnloadFunction);

然后,您随时可以拨打OnloadFunction.

Then you can call OnloadFunction whenever you want to.

这篇关于Ajax发布后执行document.ready的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆