javascript - 移除 Spinner 动画 Boostrap / JS
问题描述
我正在尝试在显示结果后删除微调器动画。
<script>
var url = "TEST";
$(document).ready(function () {
document.getElementById('spinner').setAttribute('style', 'display:none')
$("button").click(function () {
document.getElementById('response').setAttribute('style', 'display:none')
var searchText = document.getElementById('searchText').value;
var countryCode = document.getElementById('countryCode').value;
$('#btn').html('<span class="spinner-border spinner-border-sm mr-2" role="status" aria-hidden="true" id"loading"></span>En cours...').addClass('disabled');
$.ajax({
type: 'POST',
url: url,
data: {
'query': searchText,
'country_code': countryCode
},
success: function (response) {
console.log(response);
document.getElementById('response').setAttribute('style', 'display:block')
document.getElementById('spinner').setAttribute('style', 'display:none');
create_response(response);
},
error: function (data) {
document.getElementById('spinner').setAttribute('style', 'display:none');
document.getElementById('btn').setAttribute('style', 'display:block');
alert('Error! Try again..');
console.log(data);
},
});
});
});
<div class="d-flex justify-content-center"><button id="btn" class="btn btn-primary" >Extraire</button></div>
<div class="d-flex justify-content-center"><button id ="spinner" class="btn btn-primary" type="button" disabled>
<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>
En cours...
</button>
Spinner 在结果后仍然显示。我不知道如何恢复按钮。
请注意,一些代码已被删除。
谢谢您的帮助
解决方案
您需要将按钮的状态恢复到添加微调器之前的状态。
<script>
var url = "TEST";
$(document).ready(function () {
document.getElementById('spinner').setAttribute('style', 'display:none')
$("button").click(function () {
document.getElementById('response').setAttribute('style', 'display:none')
var searchText = document.getElementById('searchText').value;
var countryCode = document.getElementById('countryCode').value;
$('#btn').html('<span class="spinner-border spinner-border-sm mr-2" role="status" aria-hidden="true" id"loading"></span>En cours...').addClass('disabled');
$.ajax({
type: 'POST',
url: url,
data: {
'query': searchText,
'country_code': countryCode
},
success: function (response) {
console.log(response);
$('#btn').html('Extraire').removeClass('disabled');
create_response(response);
},
error: function (data) {
$('#btn').html('Extraire').removeClass('disabled');
alert('Error! Try again..');
console.log(data);
},
});
});
});
</script>
此外,该disable
课程并没有阻止您的按钮被使用,不确定这是否是故意的。
要正确禁用按钮,请使用以下代码:
$('#btn').attr("disabled", true);
当您完成加载结果(或出现错误时)时,再次将该值设置为 false。
推荐阅读
- flutter - 如何在 Flutter 的屏幕顶部创建一个半椭圆形?
- python - 无法运行程序“/../../python.exe”(在目录“/PythonSoftwareFoundation.Python.3.8_3.8.240.0_x64__qbz5n2kfra8p0”中):访问被拒绝
- python - 我正在尝试在 Python 中实现 NFA 来识别单词,但我的代码不起作用,
- python - 使用 while 循环检查状态是否更改
- c - 为什么我的程序可以使用 for 但现在使用 while 循环?
- node.js - 为什么需要在 Linux 上才能在 Nodejs 上构建网站并做出反应?
- c# - 当我想获得输入时出现错误
- python-3.x - 尝试使用音乐数据进行迁移学习
- python - 如何在 python 3.8 中安装 opencv-python
- javascript - ReferenceError:“未定义 Swal”