简体   繁体   English

为什么我不能在页面内加载 html 内容(来自 php 文件)?

[英]Why can't I load html content (from a php file) inside the page?

I want the content of the page chosen by select to be loaded into the ajax-container div, but this is not working, why?我希望select选择的页面内容加载到ajax-container div中,但这不起作用,为什么?

index.php索引.php

 <!doctype html> <html lang="pt-BR"> <head> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /> <title>INTRANET - REDES UNIPAM</title> <link rel="stylesheet" type="text/css" href="../css/style.css" /> <link rel="stylesheet" href="../css/bootstrap.min.css" /> <script src="../js/bootstrap.min.js"></script> <script src="../js/jquery-3.5.1.min.js"></script> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /> </head> <body> <div id="menu"> <?php include("../menu.php");?> </div> <div class="main"> <div class="container"> <div class="row"> <div class="col-6"> <fieldset class="scheduler-border"> <legend class="scheduler-border">Redes do UNIPAM</legend> <select id="mySelect" class="custom-select"> <option selected>Selecionar rede</option> <option><a class="ajax-click" href="index_bloco_a.php">Bloco A</a></option> <option><a class="ajax-click" href="index_bloco_b.php">Bloco B</a></option> </select> </fieldset> </div> </div> <div class="row"> <div class="col-6" id="ajax-container"> </div> </div> </div> </div> <script> $('.ajax-click').click(function(e) { var $clickedElement = $(this), pageToLoad = $clickedElement.prop("href"); $('#ajax-container').load(pageToLoad, function() { alert('Conteudo carregado com sucesso'); }); e.preventDefault(); }); </script>"; </body> </html>

index_bloco_a.php index_bloco_a.php

<table width="1500px" border="0" align="center">
        <tr> <td align="center" bgcolor="#EAEAEA"><titulo><strong>BLOCO A</strong></titulo></td> </tr>
</table>
<table align="center" border="0" cellpadding="8">
        <tr align="center"> <td> <a href="bloco_a_geral.php"> GERAL </a> </td> </tr>
    <tr align="center"> <td> <a href="bloco_a_auto_atendimento.php">AUTO ATENDIMENTO </a> </td> </tr>
    
</table>

I wondered if it was because the <script> tag was inside a php file, and I tried putting it inside a <? Php echo "<script> </script>"?>我想知道是不是因为<script>标签在一个 php 文件中,我试着把它放在一个<? Php echo "<script> </script>"?> <? Php echo "<script> </script>"?> , But that was just the blank page. <? Php echo "<script> </script>"?> ,但这只是空白页。

  1. You need to load jQuery BEFORE bootstrap您需要在引导之前加载 jQuery
  2. you want to remove the "; from </script>";你想删除"; from </script>";
  3. Selects cannot contain links选择不能包含链接

This will work better这样效果会更好

$("#mySelect").on("change", function() {
  const pageToLoad = this.value;
  if (pageToLoad) {
    $('#ajax-container').load(pageToLoad, function() {
      console.log('Conteudo carregado com sucesso');
    });
  }
})
<select id="mySelect" class="custom-select">
  <option value="" selected>Selecionar rede</option>
  <option value="index_bloco_a.php">Bloco A</option>
  <option value="index_bloco_b.php">Bloco B</option>
</select>

暂无
暂无

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

相关问题 如何使用PHP将HTML内容保存并加载到文件中 - How can I save and load the content of HTML into the file using PHP 为什么在WordPress中我无法使用functions.php文件中的钩子来加载JavaScript? - Why in WordPress I can't load my JavaScripts using an hook inside my functions.php file? 我可以使用JS从PHP / HTML代码中的另一个文件加载数据吗? - Can I use JS to load data from another file inside PHP/HTML code? 无法从html文件内部运行php代码 - Can't run php code from inside html file PHP DOM解析器中断页面并且无法加载页面内容 - PHP DOM parser breaks the page and can't load page content 为什么我不能将CSV文件中的新项目添加到PHP的while循环内的数组中? - Why I can't add a new item from a CSV file into an array inside a while loop in PHP? 如何使用 PHP 加载外部页面并替换该页面上的内容? - How can I load an external page with PHP and replace content on that page? 使用javascript在html页面内加载php页面 - load a php page inside a html page with javascript 如何使用jQuery .append和window.location将内容从单独的.php / html文件加载到div中? - How do i load content from a seperate .php/html file into a div with jQuery .append and window.location? 为什么在WP主题中,如果我从functions.php加载SlideShow(JQuery FlexSlider)不能工作,但是如果我从footer.php文件中加载它,则它可以工作吗? - Why in WP theme a SlideShow (JQuery FlexSlider) can't work if I load it form functions.php but work if I load it from my footer.php file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM