0
點擊按鈕時,我試圖將數據保存到localstorage。但是當我單擊按鈕鍵總是0,我更新記錄,不要創建新的記錄,因爲鍵= 0.我嘗試增加鍵值通過關閉,但我很新的JS和我的「返回」不起作用。 (當點擊按鈕時更改鍵值
function() {
window.onload = function() {
var key = 0;
document.getElementById('buttonCreate').onclick = function() {
var topicValue = document.getElementById("create-topic").value;
var statusValue = document.getElementById("create-status").value;
var descriptionValue = document.getElementById("create-description").value;
var storage = new Storage();
var ticket = {
topic: topicValue,
status: statusValue,
description: descriptionValue
};
storage.set(key, ticket);
return (function() {
return ++key;
}());
}
}
})();
function Storage() {
this._ITEMS_DESCRIPTOR = 'items';
}
Storage.prototype.get = function() {
var fromStorage = localStorage.getItem(this._ITEMS_DESCRIPTOR);
return fromStorage ? JSON.parse(fromStorage) : [];
};
Storage.prototype.set = function(key, items) {
localStorage.setItem(key, JSON.stringify(items));
};
爲什麼'set'需要使用給定的關鍵,但'GET'總是使用'_ITEMS_DESCRIPTOR' ?怎麼做你用'set'檢索你保存的項目? – Barmar