【发布时间】:2020-07-10 15:13:51
【问题描述】:
我做了一个简单的网站来管理和显示项目。对于我的问题,知道有 3 个文件就足够了:
-
admin-dashboard.php显示项目和管理功能(减少数量,增加 ecc。)
<?php
include("includes/header.php");
include("dbqueries/db-connection.php");
include("includes/alerts.php");
?>
...
-
dashboard-utilities.js包含实现第 1 点的所有 JavaScript 函数
...
// Reduce quantity button script
function reduceQuantity(item) {
$.ajax({
method: 'get',
url: '../dbqueries/reduceQuantity.php',
data: {
'current_item': item,
},
success: function() {
//alert("Quantità di " + "\"" + item + "\"" + " diminuita!");
$('#reduceqty-modal').modal('show');
// Reload page after closing the modal alert
$('#reduceqty-modal').on('hidden.bs.modal', function () {
window.location.reload();
})
}
});
}
...
-
alerts.php将包含所有引导模式警报以显示操作成功(即“数量已更新!”)
<!-- Reduce quantity modal -->
<div id="reduceqty-modal" class="modal" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Quantità ridotta</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>Modal body text goes here.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary">Save changes</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
...
所以问题是reduceQuantity 函数的“成功”部分不起作用。特别是不显示模态。
我认为 jQuery 找不到它的选择器,因为它在包含的文件中 (alerts.php)。我是这么认为的,因为如果我将警报代码直接放入主文件 admin-dashboard.php 一切正常。
提前谢谢你!
【问题讨论】:
-
dashboard-utilities.js如何(以及在何处)被收录?某处有<script标签吗? -
_"我认为 jQuery 找不到它的选择器,因为它在包含的文件中 (alerts.php)") ...这应该不是问题,只要来自警报的 HTML .php 在执行
reduceQuantity()函数之前加载到页面中。并且应该是,如果所有这些文件都包含在 PHP 中,那么它们应该同时发送到浏览器并在页面首次加载时加载。唯一的问题是如果reduceQuantity()在脚本标签加载时立即执行,在浏览器有机会加载模态的 HTML 之前。 -
P.S.你的控制台有什么错误吗? “成功”部分中的其他代码是否有效?例如如果您在其中添加其他命令(例如到 alert() 或 console.log)
-
首先谢谢你。
dashboard-utilities.js包含在<body>关闭admin-dashboard.php之前。控制台没有错误。reduceQuantity有效,唯一无效的是模态。 -
涵盖这一点:“我认为 jQuery 找不到它的选择器,因为它在包含的文件 (alerts.php) 中。” - jquery 看到浏览器看到的内容- 这是一个 single HTML 文件。只要“包含的文件”正在输出,那么 jquery 就能够看到它 - 所以看起来它没有被包含/输出。尝试查看源代码以查看您的模态是否存在。您还可以删除一些类(例如 class=modal),您应该会在呈现的输出中看到它。
标签: javascript php jquery jquery-selectors