2017-08-14 107 views
0

我在表中添加行並刪除,這工作正常。使用oninput=""事件,我也可以通過調用javascript函數來計算總成本。 現在,我添加<form></form>的那一刻,既不能添加行也不能向前移動。我是JavaScript新手,不知道發生了什麼。請幫助別人。oninput event in html works fine <form></form> is added?

<div class="container"> 
    <p>Add and Delete Items with Total Cost Value</p> 
    <form> 
     <div id="tableDiv"> 
      <table id="myTableHead"> 
       <tr> 
        <th>Item Name</th> 
        <th>Item Cost</th> 
       </tr> 
       <tr> 
        <td><input type="text" name="ItemName[]" id="ItemName" /></td> 
        <td><input class="ItemCostClass" type="number" name="ItemCost[]" oninput="myTotalFunction()" id="ItemCost" /></td> 
       </tr> 
      </table> 
      <table id="myTable"> 

      </table> 
      <table id="myTableTot"> 
       <tr> 
        <td><input type="text" name="Total" value="Total Cost Value --->" readonly /></td> 
        <td><input type="number" name="TotalValue" id="TotalValue" value=0 readonly /></td> 
       </tr> 
      </table> 
     </div> 
     <br> 
     <button onclick="myCreateFunction()">Create row</button> 
     <button onclick="myDeleteFunction()">Delete row</button> 
    </form> 
</div> 

<script> 
    function myCreateFunction() { 
     var TotalCostValueCurrent = parseFloat(document.getElementById("TotalValue").value); 
     if (TotalCostValueCurrent <= 25000) { 
      var table = document.getElementById("myTable"); 
      var row = table.insertRow(-1); 
      var cell1 = row.insertCell(0); 
      var cell2 = row.insertCell(1); 
      cell1.innerHTML = '<input type="text" name="ItemName[]" id="childItemName" />'; 
      cell2.innerHTML = '<input class="ItemCostClass" type="number" name="ItemCost[]" oninput="myTotalFunction()" id="childItemCost" />'; 
     } else { 
      window.alert("Sorry, You Have Reached Max Shipping Value Limit of 25000, please reduce Pieces to Max Value of 25000"); 
     } 

    } 

    function myTotalFunction() { 
     var arrItemCost = document.getElementById("tableDiv").getElementsByClassName("ItemCostClass"); 
     var arrLen = arrItemCost.length; 
     var i = 0; 
     var itemCostSum = 0; 
     while (i <= arrLen && itemCostSum <= 25000) { 
      if (itemCostSum <= 25000) { 
       itemCostSum = itemCostSum + parseFloat(arrItemCost[i].value); 
       i++; 
       document.getElementById("TotalValue").value = Math.ceil(itemCostSum); // Update Total Value 
      } 
     } 
    } 

    function myDeleteFunction() { 
     var arrItemCost = document.getElementById("tableDiv").getElementsByClassName("ItemCostClass"); 
     var arrLen = arrItemCost.length; 

     var TotalValueCurrent = parseFloat(document.getElementById("TotalValue").value); 
     var itemCostFinal = 0; 
     itemCostFinal = TotalValueCurrent - parseInt(arrItemCost[arrLen-1].value); 

     //FINAL OUTPUT 
     document.getElementById("myTable").deleteRow(-1); // Delete Last Row 
     document.getElementById("TotalValue").value = Math.ceil(itemCostFinal); // Final Cost Value 
     document.getElementById("tableDiv").getElementsByClassName("ItemCostClass").pop(); // Drop last value of Item Array 
    } 
</script> 

回答

0

在表單標籤按鈕內部傾向於執行提交的默認操作。

所以從改變你的按鈕,

<button onclick="myCreateFunction()">Create row</button> 
<button onclick="myDeleteFunction()">Delete row</button> 

<input type="button" onclick="myCreateFunction()" value="Create row"> 
<input type="button" onclick="myDeleteFunction()" value ="Delete row"> 
+0

千恩萬謝。它現在工作正常。 – docneogi

+0

沒問題總是愛幫忙。 – Sand

0

您必須添加輸入標籤,而不是按鈕標籤。由於表單中的按鈕標籤指定了默認的提交事件,因此您可以在添加任何行並刷新時提交表單。

<div class="container"> 
 
\t \t <p>Add and Delete Items with Total Cost Value</p> 
 
\t \t <form> 
 
\t \t \t <div id="tableDiv"> 
 
\t \t \t \t <table id="myTableHead"> 
 
\t \t \t \t \t <tr> 
 
\t \t \t \t \t \t <th>Item Name</th> 
 
\t \t \t \t \t \t <th>Item Cost</th> 
 
\t \t \t \t \t </tr> 
 
\t \t \t \t \t <tr> 
 
\t \t \t \t \t \t <td><input type="text" name="ItemName[]" id="ItemName" /></td> 
 
\t \t \t \t \t \t <td><input class="ItemCostClass" type="number" name="ItemCost[]" oninput="myTotalFunction()" id="ItemCost" /></td> 
 
\t \t \t \t \t </tr> 
 
\t \t \t \t </table> 
 
\t \t \t \t <table id="myTable"> 
 

 
\t \t \t \t </table> 
 
\t \t \t \t <table id="myTableTot"> 
 
\t \t \t \t \t <tr> 
 
\t \t \t \t \t \t <td><input type="text" name="Total" value="Total Cost Value --->" readonly /></td> 
 
\t \t \t \t \t \t <td><input type="number" name="TotalValue" id="TotalValue" value=0 readonly /></td> 
 
\t \t \t \t \t </tr> 
 
\t \t \t \t </table> 
 
\t \t \t </div> 
 
\t \t \t <br> 
 
\t \t \t <input type="button" onclick="myCreateFunction()" value="Create row" /> 
 
\t \t \t <input type="button" onclick="myDeleteFunction()" value="Delete row" /> 
 
\t \t </form> 
 
\t </div> 
 

 
\t <script> 
 
\t function myCreateFunction() { 
 
\t \t var TotalCostValueCurrent = parseFloat(document.getElementById("TotalValue").value); 
 
\t \t if (TotalCostValueCurrent <= 25000) { 
 
\t \t \t var table = document.getElementById("myTable"); 
 
\t \t \t var row = table.insertRow(-1); 
 
\t \t \t var cell1 = row.insertCell(0); 
 
\t \t \t var cell2 = row.insertCell(1); 
 
\t \t \t cell1.innerHTML = '<input type="text" name="ItemName[]" id="childItemName" />'; 
 
\t \t \t cell2.innerHTML = '<input class="ItemCostClass" type="number" name="ItemCost[]" oninput="myTotalFunction()" id="childItemCost" />'; 
 
\t \t } else { 
 
\t \t \t window.alert("Sorry, You Have Reached Max Shipping Value Limit of 25000, please reduce Pieces to Max Value of 25000"); 
 
\t \t } 
 

 
\t } 
 

 
\t function myTotalFunction() { 
 
\t \t var arrItemCost = document.getElementById("tableDiv").getElementsByClassName("ItemCostClass"); 
 
\t \t var arrLen = arrItemCost.length; 
 
\t \t var i = 0; 
 
\t \t var itemCostSum = 0; 
 
\t \t while (i < arrLen && itemCostSum <= 25000) { 
 
\t \t \t if (itemCostSum <= 25000) { 
 
\t \t \t \t itemCostSum = itemCostSum + parseFloat(arrItemCost[i].value); 
 
\t \t \t \t i++; 
 
\t \t \t \t document.getElementById("TotalValue").value = Math.ceil(itemCostSum); // Update Total Value 
 
\t \t \t } 
 
\t \t } 
 
\t } 
 

 
\t function myDeleteFunction() { 
 
\t \t var arrItemCost = document.getElementById("tableDiv").getElementsByClassName("ItemCostClass"); 
 
\t \t var arrLen = arrItemCost.length; 
 

 
\t \t var TotalValueCurrent = parseFloat(document.getElementById("TotalValue").value); 
 
\t \t var itemCostFinal = 0; 
 
\t \t itemCostFinal = TotalValueCurrent - parseInt(arrItemCost[arrLen-1].value); 
 

 
\t \t //FINAL OUTPUT 
 
\t \t document.getElementById("myTable").deleteRow(-1); // Delete Last Row 
 
\t \t document.getElementById("TotalValue").value = Math.ceil(itemCostFinal); // Final Cost Value 
 
\t \t document.getElementById("tableDiv").getElementsByClassName("ItemCostClass").pop(); // Drop last value of Item Array 
 
\t } 
 
\t </script>

+0

嘿,非常感謝。 – docneogi

+0

絕對沒問題... –

相關問題