我开始学习 JavaScript,但我被这段代码卡住了。我试图了解用户如何使用 JSON 创建和修改信息;现在我可以输入数据,但我不知道代码应该如何使用相同的输入来修改它。有人可以帮忙吗?这是我目前的代码:
<!DOCTYPE html>
<html>
<body onload="myLoad()">
<form>
<label for="track">Track: </label>
<input type="text" name="track" id="track" required><br>
<label for="artist">Artist: </label>
<input type="text" name="artist" id="artist" required><br>
<label for="album">Album: </label>
<input type="text" name="album" id="album"><br>
<label for="genre">Genre: </label>
<select id="genre">
<option value="">--Please choose an option--</option>
<option value="rock">Rock</option>
<option value="pop">Pop</option>
<option value="jazz">Jazz</option>
<option value="classic">Classic</option>
</select><br>
<button onclick="addTrack()">Register a new track</button>
</form>
<select id="trackList" onchange="ChangeActiveUser(this.value)">
<option value="">--Please choose a track--</option>
</select>
<script>
var track = [];
function myLoad() {
let htmlSelect = document.getElementById("trackList");
htmlSelect.style.visibility = "hidden";
if (sessionStorage.getItem("hasCodeRunBefore") === null) {
// let arrayOfPersonObjects = [];
sessionStorage.setItem("tracks", JSON.stringify(track));
sessionStorage.setItem("hasCodeRunBefore", true);
} else {
track = JSON.parse(sessionStorage.getItem("tracks"));
let i = 0;
track.forEach(function(t) {
let optItem = document.createElement("option");
optItem.innerHTML = t.track;
optItem.value = i;
i = i + 1;
htmlSelect.appendChild(optItem);
});
if (i > 0) {
htmlSelect.style.visibility = "visible";
}
}
}
湖上湖
慕桂英3389331
相关分类