当使用async:false
loadmask调用的jQuery ajax未触发时。
即使我在ajax之前打电话:
$("#d").on("click", function () {
$("#d").mask("Waiting...");
fn();
$("#d").unmask("Waiting...");
});
以下完整代码:
<link href="jquery.loadmask.css" rel="stylesheet" type="text/css" />
<script src="jquery-latest.pack.js" type="text/javascript"></script>
<script src="jquery.loadmask.js" type="text/javascript"></script>
<div id="d" style="border: 1px solid red; width: 300px; height: 200px">asdfasdf</div>
<script>
$(function () {
$("#d").on("click", function () {
fn();
});
});
function fn() {
var a = JSON.stringify({
text: 'some text',
array: [1, 2, 'three'],
object: {
par1: 'another text',
par2: [3, 2, 'one'],
par3: {}
}
});
var data = {
json: a,
delay: 3
}
$.ajax({
url: "http://jsfiddle.net/echo/json/",
data: data,
type: "POST",
async: false,
beforeSend: function (response) {
$("#d").mask("Waiting...");
},
success: function (response) {
console.log(response);
},
complete: function () {
$("#d").unmask("Waiting...");
}
});
}
</script>
答案 0 :(得分:1)
这是因为浏览器线程被ajax等待阻止。解决方案是使用setTimeout()。
尝试
$("#d").on("click", function () {
$("#d").mask("Waiting...");
setTimeout(function(){
fn()
$("#d").unmask("Waiting...");
}, 1);
});
答案 1 :(得分:0)
由于您的调用是异步脚本未加载jQuery $(document).ready and UpdatePanels?
我认为这会起作用
function BindEvents(){
$("#d").on("click", function () {
$("#d").mask("Waiting...");
fn();
$("#d").unmask("Waiting...");
});
}
$(document).ready(function () {
BindEvents();
});
<script type="text/javascript">
// Create the event handler for PageRequestManager.endRequest
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_endRequest(BindEvents);
</script>