我正在使用本教程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中编写它?
答案 0 :(得分: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'];
}
}
?>