[英]How can I save a number into a javascript cookie that can be printed on the screen
我想要一个按钮,当您单击它时,它会在屏幕上显示的数字上加1。 这就是我走了多远。
<button onclick="addOne();">Click Me</button>
<input type="text" id="inc" value="0"></input>
<script>
function addOne()
{
i++;
document.getElementById('inc').value = i;
}
</script>
我如何才能将数字保存到Cookie中,以便每次回来时都不会将数字改回0?
变量i没有起始值。 您可以执行以下操作:
已更新,请参见http://jsfiddle.net/sUHy2/4/
<button onclick="addOne();">Click Me</button>
<input type="text" id="inc" value="0"></input>
<button onclick="resetCookie()">Reset Cookie</button>
<script>
function resetCookie()
{
document.cookie = 0;
writeValue();
}
function addOne()
{
document.cookie++;
writeValue();
}
function writeValue()
{
document.getElementById('inc').value = document.cookie;
}
window.onload = function(){
if(!document.cookie)
{
document.cookie = 0;
}
writeValue();
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.