データが入力されているかどうかを確認するために、「追加」ボタンを押す前に、チェックする必要があります。その後、配列の空白のエントリを受け入れ始めます。なぜ次回に失敗するのか分かりません。私はJavaスクリプトを初めて使っているので、どんな助けにも感謝しています。 ここに私のコードスニペットです。if..elseブロックが1回だけ機能する理由
<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
<title>Click the button to add a new el</title>
</head>
<body>
<label>Enter an New item to add in Stock</label>
<br>
<input type="text" name=" itemName" id="addItemInStock"></input>
<br>
<p id="errorMsg"></p>
<button onclick="addToStock()">Add</button>
<p id="showList"></p>
<p id="listCount"></p>
<script>
var fruits = ["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!!";
} else {
document.getElementById("errorMsg").style.display = "none";
fruits.push(newItem.value);
document.getElementById("showList").innerHTML = fruits;
clearAndShow();
}
}
function clearAndShow() {
newItem.value = " ";
}
</script>
</body>
</html>
' ""!=="「'これに' clearAndShow() '関数を変更してみてください動作しませんでした:'関数clearAndShow(){ newItem.value = ""; } ' –
@Kevin Kloet私はあなたが何を意味するかを知りません.. –
空の文字列は空白文字を含む文字列と同じではありません –