【发布时间】:2018-09-05 12:01:29
【问题描述】:
我阅读了following answer。基本上我有一个button,当我点击它时,它会在我的localStorage 中增加number,并将其设置为span 中的文本:
页面首次加载时:
$("#jikubox span").text(localStorage.length);
点击功能:
$(".save_post").on("click", function() {
var curId = $(".my_post_id").data("id");
$("#input_post_id").val(curId);
localStorage.setItem("attempts", "0");
var attempts = Number(localStorage.getItem("attempts"));
localStorage.setItem("attempts", ++attempts);
$("#jikubox span").text(localStorage.getItem("attempts"));
});
但如果我执行以下操作,我总是会收到 1
console.log(localStorage.getItem("attempts"));
【问题讨论】:
-
好吧,您将
attempts设置为 0,然后在您的点击处理程序中将其递增 ... -
因为每次单击按钮时,您都会在此行首先将其设置为 0(零)。
localStorage.setItem("attempts", "0");。然后通过增加它,你总是将它设置为 1。 -
@Zenoo 哎哟!没错!
标签: javascript jquery local-storage