2016-08-09 17 views
0

JavaScriptのイベントに問題があります。クリック後に入力値をクリアするコードが必要です。入力後に移入さと「mytableは」行に記載されている、ここで入力要素をクリアするには、私のコードクリックイベント後の入力値をクリアする

<main> 
<table> 
    <tr> 
     <td><input type="text" id="gds"></td> 
     <td><input type="text" id="prc"></td> 
     <td><input type="button" class="button" value=" + Add " onclick="addField();"></td> 
    </tr> 
</table> 
<table id="myTable"> 
</table> 
<script type="text/javascript"> 
function addField (argument) { 
     var myTable = document.getElementById("myTable"); 
     var currentIndex = myTable.rows.length; 
     var currentRow = myTable.insertRow(-1); 

     var x = document.getElementById("gds").value; 
     var y = document.getElementById("prc").value; 

     var NumBox = document.createElement("input"); 
     NumBox.setAttribute("name", "nmbr[]" + currentIndex); 
     NumBox.setAttribute("value", 1 + currentIndex); 
     NumBox.setAttribute("size", "2"); 

     var KeyBox = document.createElement("input"); 
     KeyBox.setAttribute("name", "goods[]"); 
     KeyBox.setAttribute("value", x); 

     var priceBox = document.createElement("input"); 
     priceBox.setAttribute("name", "price[]"); 
     priceBox.setAttribute("value", y); 

     var currentCell = currentRow.insertCell(-1); 
     currentCell.appendChild(NumBox); 

     currentCell = currentRow.insertCell(-1); 
     currentCell.appendChild(KeyBox); 

     currentCell = currentRow.insertCell(-1); 
     currentCell.appendChild(priceBox); 

     currentCell = currentRow.insertCell(-1); 
     currentCell.appendChild(addRowBox); 

     var input1 = document.getElementById("gds"); 
     input1.reset(); 

     var input2 = document.getElementById("prc"); 
     input2.reset(); 
    } 
</script> 
</main> 

答えて

0

あなたのコードは、この行でエラーがスローされます。定義されていないaddRowBoxは(そのここで誤ってのように私には思える)

currentCell.appendChild(addRowBox);

ので。さらに、.reset()を使用して入力値を消去するのではなく、その値を空の文字列に設定するだけです。

だから、これはあなたがここに持っているしたいものはおそらくです:

currentCell = currentRow.insertCell(-1); 
    //currentCell.appendChild(addRowBox); 

    var input1 = document.getElementById("gds"); 
    input1.value = ''; 

    var input2 = document.getElementById("prc"); 
    input2.value = ''; 
+0

おかげで4ウルソリューション。それは仕事です! –

1

だ後、入力フィールドが

空白行く、単に空の文字列にその値を設定します。

document.getElementById("gds").value = ""; 
document.getElementById("prc").value = ""; 
関連する問題