2016-08-09 89 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

您的代碼在這行拋出一個錯誤:

currentCell.appendChild(addRowBox);

因爲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 = "";