如果jquery脚本's在另一个php页面的php-include语句中使用

Why does a jquery script not run if it's being used in a php include statement from another php page?

本文关键字:php-include 语句 php 另一个 脚本 jquery 如果      更新时间:2023-09-26

我使用javascript和php来设置和获取跨页面的cookie。我有一个脚本,它在一个页面的cookie变量中放入一个值,我想在另一个页面上检索它。假设我的第一个页面叫做page1.html。在page1中,我使用一个脚本来存储一个cookie值,如下所示:

 <Script>
      $(function () {
         $("#country-selector").change(function(){
         $.cookie("Ron","Happy Birthday");
      });
    });
 </script>

现在假设我有一个名为page2.php的页面。在page2.php中,我有一条include语句,它加载page1.html,如下所示:

include('page1.html'); 

注意:Page1.html只做一件事。。。它有一个国家的下拉列表。如果我将浏览器直接指向page1.html并选择一个国家/地区,则会设置cookie变量。但是,如果通过page2.php使用下拉列表,则不会设置cookie变量。我检查浏览器设置中的cookie列表以进行更改。

如果脚本在另一个页面的include语句中使用,是什么阻止脚本运行?

更新以澄清:

Page1.php:

 <!DOCTYPE html>
 <html>
 <head>
 <?php session_start();?>

  <script src="jquery-1.11.1.min.js"></script>
  <script src="jquery.cookie.js"></script>
  <script src="jquery-ui.min.js"></script>
  <script src="jquery.select-to-autocomplete.js"></script>
  <link rel="stylesheet" href="jquery-ui.css">
 </head>
 <body>
 <select class="myList" name="Country" id="country-selector"  autofocus="autofocus" autocorrect="off" autocomplete="off">
   <option value="" selected="selected">Select Country</option>
  <option value="Afghanistan" data-alternative-spellings="AF افغانستان">Afghanistan</option>
 </select>
 <Script>
 $(function () {
 $("#country-selector").change(function(){
 $.cookie("Marry","Upside Down");

 });
  });

 </script>
 </body>
 </html>

Page2.php

 <html>
 <head>
 <script src="jquery-1.11.1.min.js"></script>
  <script src="jquery-ui.min.js"></script>
  <script src="jquery.select-to-autocomplete.js"></script>
  <script type="text/javascript" </script>
 <script src="jquery.cookie.js"></script>
  </head>
 <body>
 <?php include ("page1.php"); ?>
 </body>
 </html>

除了加载jquery插件的顺序外,我的代码很好。jquery.cookie.js应该最后加载。