【发布时间】:2020-09-24 05:28:40
【问题描述】:
以下示例代码的执行顺序是什么?将在完成 $(document).ready(function () { }); 之前执行 $(window).on('load', function () { })。
通过 AJAX 调用从服务器加载值后,我需要将所有文本字段值更改为大写。此页面有二十多个字段。
//Sample Code
$(window).on('load', function () {
//Some Code... to change all text field values to uppercase
alert("Window Loaded");
});
$(document).ready(function () {
//Some Code....
$.ajax({
url: "TestLoadOne",
type: "POST",
async: false,
contentType: 'application/text; charset=utf-8',
success: function (data) {
console.log('async false Ajax');
}
});
$.ajax({
url: "TestLoadOne",
type: "POST",
async: true,
contentType: 'application/text; charset=utf-8',
success: function (data) {
console.log('async false Ajax');
}
});
$.ajax({
url: "TestLoadOne",
type: "POST",
async: true,
contentType: 'application/text; charset=utf-8',
success: function (data) {
console.log('async false Ajax');
}
});
alert("Document Loaded");
});
//C# Code.
public string TestLoadOne()
{
System.Threading.Thread.Sleep(40000);
return "Hello";
}
【问题讨论】:
-
我认为这是answers 你的问题。
-
我担心 document.ready 中的 AJAX 调用。将 $(window).on("load", function(){});等待 AJAX 调用(在 document.ready 中)完成?
-
不,窗口 onload 事件不依赖于 AJAX 调用。
标签: javascript jquery