javascript - Javascript/CSS 显示/隐藏有时不工作
问题描述
我有一个加载器 gif,我正在尝试显示/隐藏 ajax 调用以指示进度。它适用于一种功能,而不适用于另一种功能,这很奇怪。
function showThis(id) {
document.getElementById(id).style.visibility = "visible";
}
function hideThis(id) {
document.getElementById(id).style.visibility = "hidden";
}
jQuery(function() {
jQuery("#testKey").click(function(event) {
// NEXT LINE DOESN'T WORK
showThis('loader2');
jQuery.ajax({
type: "POST",
dataType: "json",
url: "/api/?action=testKey",
data: {
exchange: jQuery('#setExchange').val(),
key: jQuery('#setKey').val(),
secret: jQuery('#setSecret').val()
},
success: function(data) {
if (!data.error) {
if (data.keyValid == true) {
alert("API Key is valid");
} else {
alert("Invalid key and/or secret.");
}
} else {
alert("Error: " + data.errorMsg);
}
}
});
hideThis('loader2');
return false;
});
});
jQuery(function() {
jQuery('#setSave').click(function(event) {
// THIS WORKS
showThis("loader2");
jQuery.post(
'/api/?action=bcSaveSettings', {
exchange: jQuery('#setExchange').val(),
key: jQuery('#setKey').val(),
secret: jQuery('#setSecret').val(),
},
function() {
var newKey = jQuery('#setKey').val();
var newSecret = jQuery('#setSecret').val();
var exchangeNum = jQuery('#setExchange').val();
var exchangeName = jQuery("#setExchange option:selected").text();
jQuery("#setExchange option:selected").attr('dat', newKey);
jQuery("#setExchange option:selected").attr('dat2', newSecret);
if (jQuery("#depExchange option[value='" + exchangeNum + "']").length > 0 && newKey.length < 1 && newSecret.length < 1) {
jQuery("#depExchange option[value='" + exchangeNum + "']").remove();
} else if (jQuery("#depExchange option[value='" + exchangeNum + "']").length < 1 && newKey.length > 0 && newSecret.length > 0) {
jQuery("#depExchange").append(jQuery('<option>', {
value: exchangeNum,
text: exchangeName
}));
}
hideThis("loader2");
alert("Settings saved");
}
);
event.preventDefault();
});
});
#loader1 {
background: #ecf0f1;
position: relative;
height: 100%;
visibility: hidden;
}
#loader2 {
background: #ecf0f1;
position: relative;
height: 100%;
visibility: hidden;
}
.dot {
position: absolute;
top: 50%;
left: 50%;
-webkit-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
background: #2ecc71;
border-radius: 100%;
animation-duration: 1s;
animation-name: loader_dot;
animation-iteration-count: infinite;
animation-direction: alternate;
}
@keyframes loader_dot {
0% {
width: 0px;
height: 0px;
}
to {
width: 50px;
height: 50px;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="testKey">test1</button>
<BR />
<BR />
<button id="setSave">test2</button>
<div id="loader2" style="visibility:hidden">
<div class="dot"></div>
</div>
最初我使用 jquery hide/show 但得到了相同的行为。注意:两个 ajax 调用都能正常工作,但加载器只显示 JS 注释中指示的那个。
解决方案
你需要打电话hideThis('loader2');
。
也许或者,甚至。ajax() callback
success
error
complete
例子:
showThis()
jQuery.ajax({
// `success` or `error`
complete: function () {
// call `hide` when the `callback` called
hideThis()
}
})
您的代码只需调用
show
andhide
,它不会等待ajax
执行。
推荐阅读
- html - 如何创建 ccs 下拉菜单?
- java - 在 couchbase 上发生服务器故障时更新 CAS 值
- azure-devops - Azure Devops Rest Api 获取团队当前 Sprint 的工作项和任务
- python - Python-'自动化无聊的东西'-while循环
- swift - 将 CollectionDifference 应用于 NSTableView
- r - 确定该函数中的函数名称
- pyomo - Pyomo 约束中的 For 循环
- blazor - Blazor WASM 没有达到断点
- javascript - 从 JavaScript 中的一组对象中删除重复项但在删除重复项之前合并一个字段
- office365 - MAPIInitialize() 在 2020 年 4 月 24 日更新 Office 365 后失败