gpt4 book ai didi

javascript - 数组元素不会显示在 JavaScript 的选择框中

转载 作者:行者123 更新时间:2023-12-02 13:56:41 25 4
gpt4 key购买 nike

我正在数组中添加新元素,并尝试将所有这些元素动态设置到 java 脚本中的选择框,但是每次刷新浏览器选择框都会再次变空。但是框外显示的列表不会改变浏览器刷新时。

    <html>
<label>Enter an New item to add in Stock</label> <br> </br> <input type="text" name=" itemName" id="addItemInStock"><br></br>
<p id="errorMsg"></p>
<button onclick="addToStock()">Add</button>
<p id="showList"></p>
<select id="showInDropDown">
<option disabled selected style="display: block;">Stock Items</option>
</select>
<script>
var fruitsfromLS = localStorage.getItem("fruits");
var fruits = fruitsfromLS ? JSON.parse(fruitsfromLS) : ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("showList").innerHTML = fruits;
var newItem = document.getElementById("addItemInStock");

function addToStock() {
if ((newItem.value) === "") {
document.getElementById("errorMsg").innerHTML = "Blank item cannot be added!!";
document.getElementById("errorMsg").style.display = "block";
} else {
document.getElementById("errorMsg").style.display = "none";
fruits.push(newItem.value);
//this is added extra
// localStorage.setItem("fruits", JSON.stringify(fruits));
var StoredFruits = localStorage.setItem("fruits", JSON.stringify(fruits));

document.getElementById("showList").innerHTML = fruits;
clearAndShow();
}

var sel = document.getElementById("showInDropDown");
document.getElementById("showInDropDown").innerHTML = "";
for (var i = 0; i < fruits.length; i++) {
var opt = document.createElement('option');


opt.innerHTML = fruits[i];
opt.value = fruits[i];
sel.appendChild(opt);
}
}

function clearAndShow() {
newItem.value = "";
}
</script>

</html>

最佳答案

因为您仅在 addToStock 中添加所选项目,并且仅在单击按钮时调用它。试试这个

<html>
<label>Enter an New item to add in Stock</label> <br> </br> <input type="text" name=" itemName" id="addItemInStock"><br></br>
<p id="errorMsg"></p>
<button onclick="addToStock()">Add</button>
<p id="showList"></p>
<select id="showInDropDown">
<option disabled selected style="display: block;">Stock Items</option>
</select>
<script>
var fruitsfromLS = localStorage.getItem("fruits");
var fruits = fruitsfromLS ? JSON.parse(fruitsfromLS) : ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("showList").innerHTML = fruits;
var newItem = document.getElementById("addItemInStock");

function addToStock() {
if ((newItem.value) === "") {
document.getElementById("errorMsg").innerHTML = "Blank item cannot be added!!";
document.getElementById("errorMsg").style.display = "block";
} else {
document.getElementById("errorMsg").style.display = "none";
fruits.push(newItem.value);
//this is added extra
// localStorage.setItem("fruits", JSON.stringify(fruits));
var StoredFruits = localStorage.setItem("fruits", JSON.stringify(fruits));

document.getElementById("showList").innerHTML = fruits;
clearAndShow();
}
fillSelect();

}

function fillSelect(){
var sel = document.getElementById("showInDropDown");
document.getElementById("showInDropDown").innerHTML = "";
for (var i = 0; i < fruits.length; i++) {
var opt = document.createElement('option');
opt.innerHTML = fruits[i];
opt.value = fruits[i];
sel.appendChild(opt);
}
}

function clearAndShow() {
newItem.value = "";
}

window.onload = function(){
fillSelect();
};

</script>
</html>

我将选择填充代码移至其自己的函数中,并在页面加载时使用 window.onload 调用它

关于javascript - 数组元素不会显示在 JavaScript 的选择框中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40651578/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com