2016-11-15 3 views
1

データが入力されているかどうかを確認するために、「追加」ボタンを押す前に、チェックする必要があります。その後、配列の空白のエントリを受け入れ始めます。なぜ次回に失敗するのか分かりません。私は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>

+2

' ""!=="「'これに' clearAndShow() '関数を変更してみてください動作しませんでした:'関数clearAndShow(){ newItem.value = ""; } ' –

+0

@Kevin Kloet私はあなたが何を意味するかを知りません.. –

+1

空の文字列は空白文字を含む文字列と同じではありません –

答えて

2

あなたclearAndShow機能は、単一の空間に入力要素値を設定するが、空の文字列のためifチェックします。

また、誰にも負けない、それは示して初めてdisplayを設定していますが、'none'から反対として、エラーが表示されるように必要なバックblock

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!!"; 
 
     document.getElementById("errorMsg").style.display = "block"; 
 
     } else { 
 
     document.getElementById("errorMsg").style.display = "none"; 
 
     fruits.push(newItem.value); 
 
     document.getElementById("showList").innerHTML = fruits; 
 

 
     clearAndShow(); 
 
     } 
 
    } 
 

 
    function clearAndShow() { 
 
     newItem.value = ""; 
 
    }
<label>Enter an New item to add in Stock</label> 
 
    <br> 
 
    </br> 
 
    <input type="text" name=" itemName" id="addItemInStock"></input> 
 
    <br></br> 
 
    <p id="errorMsg"></p> 
 

 
    <button onclick="addToStock()">Add</button> 
 

 
    <p id="showList"></p> 
 
    <p id="listCount"></p>

1

にそれを切り替えることはありません。

document.getElementById("errorMsg").style.display = "block"; 

var fruits = ["Banana", "Orange", "Apple", "Mango"]; 
 
document.getElementById("showList").innerHTML = fruits; 
 
var newItem = document.getElementById("addItemInStock"); 
 

 
function addToStock() { 
 
    if (newItem.value === "") { 
 
    document.getElementById("errorMsg").style.display = "block"; 
 
    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 = ""; 
 
}
<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>

+0

ありがとうございました。エラーメッセージを表示するのがなぜ奇妙なのですか? elseブロックが最初の実行後に実行を停止する場合は、 –

+0

は動作を停止しませんが、エラーを表示していません。あなたは、その部分が配列に挿入されないことを知っています。それは明らかにうまくいく。 –

+0

no..itは、arrrayの値の挿入を開始します。エラーメッセージの表示にもかかわらず、追加のクリックごとに「、」が追加されています –

0

あなたのコードは、ちょうど小さな問題がありました。 newItem.value = ""を設定すると、この条件はnewItem.value!= ""に失敗します。したがって、あなたのAddtoStock機能が正しく

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 = ""; 
} 
関連する問題