javascript - 如何在 Ajax Jquery 方法中使用 setTimeout() 防止关闭模式?
问题描述
我正在尝试使index.php
页面“自动”每 2 秒从 MySQL 表中加载数据,使用setTimeout()
. 问题是每次我从第二页(order.php
)单击查看详细信息按钮时,模式加载但 2 秒后立即关闭。
当我单击查看详细信息按钮并在关闭模式后继续刷新 div 以从 MySQL 表中加载数据时,如何防止自动关闭模式?
index.php(第一个 php 页面)
// INDEX.PHP
<body>
<div id = "showCustomerOrder_div"></div>
</body>
<script>
var interval = 2000;
function displayCustomerOrder() {
$.ajax({
type: 'POST',
url: 'order.php',
data: { },
success: function (data) {
$('#showCustomerOrder_div').html(data);
},
complete: function (data) {
setTimeout(displayCustomerOrder, interval);
}
});
}
setTimeout(displayCustomerOrder, interval);
</script>
order.php(第二个 php 页面)
// ORDER.PHP
<?php
$count = 0;
while ($rows = mysqli_fetch_array($query)) {
$count++;
$date = $rows['date_order'];
$receipt = $rows['receipt_no'];
$fullname = $rows['fullname'];
$restoname = $rows['resto_name'];
$total = $rows['Total'];
?>
<tr>
<td><?php echo $count; ?></td>
<td><?php echo date_format($date,"m/d/y h:i:s"); ?></td>
<td><?php echo $receipt; ?></td>
<td>
<a href = "#orderDetails<?php echo $receipt; ?>" data-toggle = "modal" class = "btn btn-primary">View Details</a>
</td>
</tr>
<div class = "modal" id = "orderDetails<?php echo $receipt; ?>">
<div class = "modal-dialog">
<div class = "modal-content">
<div class = "modal-header">
</div>
<div class = "modal-body">
<div class = "row">
<div class = "col"><?php echo $rows['receipt_no']; ?></div>
<div class = "col"><?php echo $rows['fullname']; ?> </div>
<div class = "col"><?php echo $rows['Total']; ?> </div>
</div>
</div>
<div class = "modal-footer">
</div>
</div>
</div>
</div>
<?php
}
?>
非常感谢!
解决方案
像这样的东西
$("#container").on("click","[data-toggle=modal]",function(e) {
e.preventDefault();
var texts = $(this).closest("tr").find("td").map((_, cell) => cell.innerText).get();
texts.pop(); // lose the link text
$(".row").empty();
$.each(texts, (_, text) => $(".row").append(`<div class="col">${text}</div>`));
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="container">
<table>
<tr>
<td>One</td>
<td>2019 05 10</td>
<td>receipt 1</td>
<td>
<a href="#" data-toggle="modal" class="btn btn-primary">View Details</a>
</td>
</tr>
<tr>
<td>Two</td>
<td>2019 05 11</td>
<td>receipt 2</td>
<td>
<a href="#" data-toggle="modal" class="btn btn-primary">View Details</a>
</td>
</tr>
</table>
</div>
<div class="modal-body">
<div class="row">
</div>
</div>
推荐阅读
- node.js - Mongoose 是否与预配置吞吐量的 Cosmos DB 一起使用?
- amazon-web-services - SQS 消息接收器中缺少 MessageAttributes 属性
- python - 如何优化 3 个单独数组上的三重嵌套 for 循环(Python)
- javascript - 加载页面后,如何在我的 c# 程序中恢复通过 cefSharp 调用的 javascript 函数发送的列表?
- javascript - 如何将 props.theme.breakpoints 与 makeStyles 挂钩使用?
- latex - APA6 一位作者乳胶引用不起作用
- swiftui - 如何增加 SwiftUI 选择器中显示的最大行数?
- optimization - Adobe 分析与 adobe 目标的集成
- python - 尝试通过列表传递for循环时出现索引超出范围错误
- rxjs - rxjs:我可以在定义 observable 之前订阅吗?