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>
I'trying做出的預訂系統。用戶必須登錄並提交報價。我試圖在註銷時保留預訂的數量,但是當我刷新頁面時它會一直重新啓動。由以前的網友提到在餅乾
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
}
找到這個完整的代碼你有沒有檢查這個文檔:https://github.com/carhartl/jquery-cookie –
[Cookies!](http://www.w3schools.com/js) /js_cookies.asp) – moffeltje
因爲在頁面加載時您不讀取cookie值。 – epascarello