1 回答

TA貢獻1846條經(jīng)驗 獲得超7個贊
感謝您更新您的帖子并包含 add_name.html 文件。主要問題就在那里。passVal 函數(shù)中所做的就是啟動一個新數(shù)組,然后向該數(shù)組添加一個值,然后將本地存儲中的列表設(shè)置為該數(shù)組。因此本地存儲中的數(shù)組始終只有一項。
您不應將 newList 變量設(shè)置為空數(shù)組,而應將其設(shè)置為本地存儲中已有的項目列表:
添加名稱.html
<script>
function passVal() {
var previousValue = localStorage.getItem("newList1"); // Get the previous value
var newList;
if(previousValue) {
newList = JSON.parse(previousValue);
} else {
newList = []; // If nothing is in the local storage until now, start with an empty list.
}
var newName = document.getElementById("addname").value;
newList.push(newName);
localStorage.setItem("newList1", JSON.stringify(newList));
}
</script>
</body>
{% endblock %}
然后在 home.html 中,您需要循環(huán)遍歷這些值:
var LL = JSON.parse(localStorage.getItem("newList1"));
for(let item of LL) {
document.getElementById("List").innerHTML += "<li>" + item + "</li>";
}
- 1 回答
- 0 關(guān)注
- 160 瀏覽
添加回答
舉報