JS:
var x = 0;
function count() {
x += 1;
document.getElementById( "counting" ).value = x;
$.cookie("key", value)
}
</script>
HTML:
<div id="product">
<div id="picture">
<a href="?page=product" style="margin-left: 0px"> <img src="1h.jpg"
alt="Online Shop" width="100%" height="100%">
</a>
</div>
<div id="title"
style="float: left; margin-left: 2%; margin-top: 5%; height: 40px; width: 70%; font-size: 22px;">
<span> Oasis Beach Hotel </span> <span style="float: right"> Price:
$50 </span>
</div>
<div id="title"
style="float: left; margin-left: 2%; margin-top: 0%; height: 20px; width: 70%; font-size: 16px;">
<span> Bookings: <input id="counting" type="text" />
</span> <span style="float: right"> <?php echo'
<form action=" ';?> <?php echo $_SERVER['PHP_SELF'];?> <?php echo' " method="post" style="margin-left: 2%">
<input type ="button" value = "ORDER" onclick = "count()"/><br><br>
</form>
';?>
</span>
</div>
</div>
我试图预订系统。用户必须登录并提交报价。我试图在注销时保留预订数量,但在刷新页面时它会不断重新启动。
答案 0 :(得分:1)
因此,在页面加载时读取cookie值
var value = parseInt($.cookie("key") || 0, 10);
答案 1 :(得分:0)
您需要在刷新时读取cookie中的值,如下所示:
if($.cookie('key')){
var x = parseInt($.cookie('key'));
}
else{
var x = 0;
}
function count() {
x += 1;
document.getElementById( "counting" ).value = x;
$.cookie("key", x); //I assume you want to save x and not val
}
答案 2 :(得分:-1)
如先前用户所述,将值存储在cookie中。 您还可以使用AJAX将值传递给PHP,然后将其存储在会话变量中。您可以在http://www.w3schools.com
上找到完整的代码