[英]Execute $(document).ready() after $.load(), is that possible or is there an Alternative
我有一個帶有表單和表格的頁面(以顯示使用表單保存的數據的結果)。
該表單使用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>
頁面的HTML完成加載后,將調用document.ready,對此沒有兩種選擇。
但是,您可以使用實時綁定,它將活動處理程序附加到頁面上尚未存在的元素上。
例:
$(".datepicker").live("click", function() {
$(this).datepicker();
})
已針對jQuery> 1.7更新(這也更快)
$("#tableOfContent").on("click", ".datepicker", function() {
$(this).datepicker();
})
你可以做
$('#tableOfContent').load("table.php",function(){
//completed load actions here
});
但是請注意,如果您加載圖像,則將不會加載它們。 在這種情況下,您可以將table.php的內容最初隱藏起來,然后在$('#tableOfContent img')。load()中再次執行相同操作。 這將適用於1張圖片; 多個圖像有點復雜,但是請隨時問您是否正在尋找它:)
您可以使用jQuery.holdReady()延遲ready事件:
$.holdReady(true);
// Do your custom stuff... the document may already be loaded.
$.holdReady(false); // Now the ready event will fire as soon as the DOM is loaded.
$(document).ready()應該用於應該在文檔准備就緒時執行的腳本。
如果您需要在ajax調用之后執行某些操作,則可以在函數中編寫所有內容,並使用ajax回調對其進行調用。
function what_i_need() {
// bla bla
}
<script>
$('#tableOfContent').load("table.php", {}, what_i_need);//code had syntax error; '{)'
</script>
我不確定。 另外,您也可以在文檔准備好后調用該函數。
$(document).ready(function(){
what_i_need();
});
從ready函數中加載表數據,並使用load()函數的complete事件來調用其余數據
$(document).ready(function() {
// click bindings etc ..
$('#tableOfContent').load("table.php",function() {
// things to do once the table is loaded
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.