javascript - 在 $.load() 之后执行 $(document).ready() 是否可能或是否有替代方案

标签 javascript jquery ajax

我有一个带有表单和表格的页面(用于显示使用表单保存的数据的结果)。

表单使用ajax提交数据,数据保存后需要重新加载表格。

问题是表(使用 AJAX($.load) 加载)是在执行 $(document).ready() 后加载的。这意味着该表不具备所需的功能。

是否有任何方法可以推迟执行 $(document).ready() 直到 AJAX 完成加载,或者我应该使用完全不同的方法,例如使用 iframe?

下面是我的问题的示例:

 $(document).ready(function(){
    //some code here that needed for the html in table.html e.g. datepicker, chosen, jqueryui, etc 
 });
 <form>
    //Inputs with a button to submit using ajax, where the result is displayed using table.php
 </form>
 <div id="tableOfContent"></div> 
    <script>
       $('#tableOfContent').load("table.php");
    </script>

最佳答案

你可以做到

$('#tableOfContent').load("table.php",function(){ 
   //completed load actions here
});

但是你应该注意,如果你加载图像,它们还不会被加载。如果是这种情况,您可以将 table.php 的内容最初隐藏,然后在 $('#tableOfContent img').load() 中再次执行相同操作。这适用于 1 张图像;多个图像有点复杂,但请随时询问这是否是您正在寻找的:)

关于javascript - 在 $.load() 之后执行 $(document).ready() 是否可能或是否有替代方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8428502/

相关文章:

javascript - 根据特定div的属性更改正文背景

javascript - Paper.js - 将 `paperscript` 转换为 `javascript`

javascript - 使用 .remove() 删除数组中的所有元素

javascript - react -Redux : Passing arguments from handler to action creator

javascript - 如何在PWA和ReactJS中创建动态manifest.json文件?

jquery - 向远程验证属性 MVC3 添加延迟

jquery - 添加类 "selected"

jquery - 即使设置了超时,也无法使 jQuery ajax 调用持续超过 4 分钟,并出现 net::ERR_EMPTY_RESPONSE 错误

javascript - 在 AJAX 请求后,Chrome 在 HTTP 205 响应上下载重新加载的页面

javascript - 无服务器 CORS 错误 : Did not find method in CORS header ‘Access-Control-Allow-Methods'