简体   繁体   English

包含的php文件中的jQuery选择器不起作用

[英]jQuery selector in included php file not working

I made a simple website to manage and display items.我做了一个简单的网站来管理和显示项目。 For my problem is sufficient to know that there are 3 files:因为我的问题足以知道有 3 个文件:

  1. admin-dashboard.php which display items and admin functionalities (reduce quantity, increase ecc.) admin-dashboard.php显示项目和管理功能(减少数量,增加 ecc。)

 <?php include("includes/header.php"); include("dbqueries/db-connection.php"); include("includes/alerts.php"); ?> ...

  1. dashboard-utilities.js which contains all the JavaScript functions to realize point 1 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(); }) } }); } ...

  1. alerts.php which will contain all the bootstrap modal alerts to display on operations success (ie "Quantity updated!") 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">&times;</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> ...

So the problem is that the "success" part of the reduceQuantity function is not working.所以问题是reduceQuantity函数的“成功”部分不起作用。 In particular the modal is not displayed.特别是不显示模态。

I supposed that jQuery cannot find its selector cause it is in the included file ( alerts.php ).我认为 jQuery 找不到它的选择器,因为它在包含的文件( alerts.php )中。 I think so because if I put the alert code directly into the main file admin-dashboard.php everything works.我这么认为是因为如果我将警报代码直接放入主文件admin-dashboard.php一切正常。

Thank you in advance!先感谢您!

... The problem was related to the server. ... 问题与服务器有关。 The file alerts.php didn't have the correct permissions and the correct owner.文件alerts.php没有正确的权限和正确的所有者。 I noticed it when you said that it wasn't probably really included.当你说它可能并不真正包括在内时,我注意到了。

Sorry for wasting your time, thank you all!抱歉耽误大家时间了,谢谢大家!

It's sounds like the reduceQuantity() function cannot find the modal element, because the function is running before the HTML element has been parsed.听起来reduceQuantity()函数找不到模态元素,因为该函数在解析 HTML 元素之前正在运行。 This could either be because you need to wrap your JQuery in a $(document).ready(function(){}) or you need to include the Javascript at the end of your admin-dashboard.php file (before the closing </body> tag) and then put the alerts.php at the top so that it can be parsed before the Javascript runs.这可能是因为您需要将 JQuery 包装在$(document).ready(function(){})或者您需要在admin-dashboard.php文件的末尾包含 Javascript(在结束之前</body>标记),然后将alerts.php放在顶部,以便在 Javascript 运行之前可以对其进行解析。 You might need to do both of these things.您可能需要同时执行这两件事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM