0

我正在使用本教程http://www.w3schools.com/jquery/tryit.asp?filename=tryjquery_slide_toggle

这是没有样式的代码。

<script type="text/javascript"> 
$(document).ready(function()
{


$(".flip").click(function()
    {
    var panel = "open";
    $(".panel").slideToggle("slow");
    });
});
</script>

我将如何保存它的状态,所以如果我刷新页面,它将保持打开或关闭状态。我想一个 php 会话将是正确的方法,但我该如何在 javascript 中编写它呢?

4

1 回答 1

0

在 JS 中:

var readWirite='write'; //or 'read'
$.ajax({
   type: "POST",
   url:  "myPhpFile.php",
   data: "panel="+panel+"&readWrite="+readWrite;
   success: function(msg){
      if(msg == '1'){
        alert('Horay panel saved!');
      } else {
        $('#panelId').html(msg); //Write saved panel back to html 
      }
   }                       
}); 

在 myPhpFile.php 中:

<?php
   if(!isset($_SESSION)) session_start();
   if(isset($_POST['readWrite']) && isset($_POST['panel'])){
      if($_POST['readWrite'] == 'write'){
          $result = '0'; 
          if($_SESSION['panel'] = $_POST['panel']) $result = '1';
          echo $result;
      } else if($_POST['readWrite'] == 'read') {
          echo $_SESSION['panel'];
      }
   }
?>
于 2012-05-01T05:05:48.100 回答