gpt4 book ai didi

javascript - jQuery 没有正确隐藏/显示元素

转载 作者:太空宇宙 更新时间:2023-11-04 11:45:48 26 4
gpt4 key购买 nike

我的网页模板中有以下 html 和 jQuery 代码。

$.getJSON 调用可能需要一些时间,因此我想在加载数据时显示警告(带有 id="warning"的 div),以便用户知道等待它。

代码正确验证了状态下拉列表,并将返回正确的表格数据,表格显示正常。

但是,当单击提交按钮时,警告段落不会出现,并且在加载数据时表格仍然可见。

如何让表格在加载数据时消失并同时显示警告?

  <div class="container-fluid">
<form id="search_form">
<div class="container-fluid">
<div class="row">
<div class="col-xs-4">
{{ render_field(form.status) }}
</div>
<div class="col-xs-4">
<input type="submit" value="Get Issues" id="submit">
</div>
<div class="col-xs-4">
</div>
</div>
<div class="row">
<div hidden="true" id="warning" class="col-xs-12 center alert alert-warning">
<p>Data is loading, this might take a few seconds</p>
</div>
</div>
<div class="row">
<div id="errors" hidden="true" class="col-xs-12 center alert alert-danger">
</div>
</div>
</div>
</form>

  <script>
var table;
$('#submit').click(function(event) {
event.preventDefault();

if (!$('#status').val()) {
$('#errors').html('');
$("<p>Please select a status</p>").appendTo($('#errors'));
$("#errors").show();
$("#table_div").hide();
return;
} else {
$("#errors").hide();
}

$("#warning").show();
//get the data for the table
$.getJSON("/all_issues/status/" + $("#status").val(), function(data) {
table = $('#search_table').dataTable( {
destroy: true,
"data": data,
});
$('#table_div').show();
});
$("#warning").hide();
});

最佳答案

移动$("#warning").hide()在你的里面 $.getJSON()回调。

$.getJSON() 是一个 AJAX 调用,所以回调是异步执行的。在您当前的设置中,您隐藏了 <div id="warning">在你显示它之后,当你想在你的 JSON 加载后隐藏它时。

关于javascript - jQuery 没有正确隐藏/显示元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31013515/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com