我试图在单击按钮时显示加载图像,该按钮将通过 ajax 验证表单,但加载图像仅在一些延迟后显示(上传到服务器时显示图像需要 2 秒以上)。但我希望它在按钮单击事件的那一刻。我的代码如下。
$("#Save").click(function() {
// shows theloading image (here I am expecting the loading image, it never shows.)
$("#load_img").show();
// calls ajax for vaidation
lookUpValidation($("#item1"))
// calls ajax for vaidation
lookUpValidation($("#item2"))
})
function lookUpValidation(this_element,id) {
url = "/validate/lookupid/"+id,
lookUpValidationResult = false;
lookUpValidationData = false;
if(this_element.val()!="") {
$.ajax({
url : url,
type : "POST",
async : false,
data : {
"value" : this_element.val()
},
beforeSend : function(data) {
// shows theloading image
$("#load_img").show();
},
complete : function(data) {
if(data) {
var res = JSON.parse(data.responseText);
lookUpValidationData = res;
if(res.field) {
lookUpValidationResult = 1;
}
else
// not found
lookUpValidationResult = 2;
}
else {
// not found
lookUpValidationResult = 2;
}
}
});
}
else {
// null
lookUpValidationResult = 3;
}
return lookUpValidationResult;
}
HTML
<div class="k-loading-image" id="load_img" style="display:none;">
<div class="k-loading-color">
</div>
</div>