2017-02-10 37 views
0

我正在研究計算服務總價格的代碼。添加具有唯一ID的動態輸入字段進行計數

現在,如果我添加小時數(如2)並添加每小時價格(如20),則代碼必須計算將成爲小計的價格。之後,它會計算「BTW」(稅),並將其添加到總價的小計中。

我想要的是添加動態新的輸入字段與唯一的ID,所以代碼可以計算多個服務。因此,對於每項服務,您的總金額合計爲小計。我現在代碼:

HTML

<table class="table-responsive table" id="table-diensten"> 

    <thead> 
     <tr> 
      <th>Time</th> 
      <th>Service</th> 
      <th>amount</th> 
      <th>total</th> 
      <th>BTW</th> 
     </tr> 
    </thead> 
    <tbody class="table-body"> 
     <tr class="table-row"> 
      <td><input type="text" class="form-control" placeholder="time (in hours)" id="time" onchange="totalofferte()"></td> 
      <td><input type="text" class="form-control" placeholder="service"></td> 
      <td><input type="text" class="form-control" placeholder="Cost (per hour)" id="cost" onchange="totalofferte()"></td> 
      <td>&euro; <span id="total">0,00</span></td> 
      <td>21%</td> 
     </tr> 
    </tbody> 
    <tfoot> 
     <tr> 
      <td> </td> 
      <td> </td> 
      <td><strong>Subtotaal</strong></td> 
      <td>&euro; <span id="subtotal">0,00</span></td> 
      <td> </td> 
     </tr> 
     <tr> 
      <td> </td> 
      <td> </td> 
      <td>21% BTW</td> 
      <td>&euro; <span id="costbtw">0,00</span></td> 
      <td> </td> 
     </tr> 
     <tr> 
      <td> </td> 
      <td> </td> 
      <td class="table-total"><span class="total">Totaal</span></td> 
      <td class="table-total"><span class="total">&euro; <span id="totalofferte">0,00</span></span></td> 
      <td> </td> 
     </tr> 
    </tfoot> 


</table> 

<a href="#table-diensten" class="add-tablerow btn btn-default" >add new service</a> 

JS

<script type="text/javascript"> 


function totalofferte() { 

    var cost = document.getElementById('cost').value; 
    var time = document.getElementById('time').value; 

    if (cost > 0 && time > 0) { 

     var total = cost * time; 

     if (total > 0) { 
      document.getElementById('total').innerHTML = total; 

      var subtotal = total; 

      if (subtotal > 0) { 
       document.getElementById('subtotal').innerHTML = subtotal; 

       var costbtw = subtotal/100 * 21; 

       document.getElementById('costbtw').innerHTML = costbtw; 

       var totalofferte = subtotal + costbtw; 

       document.getElementById('totalofferte').innerHTML = totalofferte; 
      } 


     } 

    } 



} 

</script> 

編輯:

忘記我的JQuery

$(".add-tablerow").click(function(){ 
    $(".table-body").append("<tr class='table-row'><td><input type='text' class='form-control' placeholder='Tijd'></td><td><input type='text' class='form-control' placeholder='Omschrijving'></td><td><input type='text' class='form-control' placeholder='Kosten'></td><td>&euro; 0,00</td><td>21%</td></tr>"); 
}); 
+0

爲什麼不用class而不是id?這將解決您的問題 –

+0

我的問題是,我知道如何添加另一行,但我不知道如何添加一個動態id /類到行中的輸入,所以我可以在我的JS調用它 – Gijsberts

+0

也許在這個答案中的源代碼將幫助你? HTTP://計算器。com/questions/35669941/calulate-function-in-javascript-for-dynamic-added-field/35676764#35676764這個動態創建的元素和計算每個價格與小計。 – NewToJS

回答

3

繼續使用addNewRow方法就可以達到你期待

function addNewRow(){ 
 
    var presentRows = $("#table-diensten > tbody > tr"); 
 
    var newRowId = presentRows.length + 1; 
 
    $("#table-diensten").append(
 
    '<tr id="' + newRowId + '">' + 
 
    '<td><input class="form-control" type="number" name="time_' + newRowId + '" id="time_' + newRowId + '"/></td>' + 
 
    '<td><input class="form-control" type="number" name="service_' + newRowId + '" id="service_' + newRowId + '"/></td>' + 
 
    '<td><input class="form-control" type="number" name="amount' + newRowId + '" id="amount' + newRowId + '"/></td>' + 
 
    '<td></td>' + 
 
    '<td></td>' + 
 
    '</tr>' 
 
); 
 
} 
 

 
function totalofferte() { 
 

 
    var cost = document.getElementById('cost').value; 
 
    var time = document.getElementById('time').value; 
 

 
    if (cost > 0 && time > 0) { 
 

 
     var total = cost * time; 
 

 
     if (total > 0) { 
 
      document.getElementById('total').innerHTML = total; 
 

 
      var subtotal = total; 
 

 
      if (subtotal > 0) { 
 
       document.getElementById('subtotal').innerHTML = subtotal; 
 

 
       var costbtw = subtotal/100 * 21; 
 

 
       document.getElementById('costbtw').innerHTML = costbtw; 
 

 
       var totalofferte = subtotal + costbtw; 
 

 
       document.getElementById('totalofferte').innerHTML = totalofferte; 
 
      } 
 

 

 
     } 
 

 
    } 
 

 

 

 
}
.navigation { 
 
    width: 300px; 
 
} 
 

 

 
.mainmenu, .submenu { 
 
    list-style: none; 
 
    padding: 0; 
 
    margin: 0; 
 
} 
 

 
.mainmenu a { 
 
    display: block; 
 
    background-color: #CCC; 
 
    text-decoration: none; 
 
    padding: 10px; 
 
    color: #000; 
 
} 
 

 

 
.mainmenu li:hover a, 
 
.mainmenu li.active a { 
 
    background-color: #C5C5C5; 
 
} 
 

 
.mainmenu li.active .submenu { 
 
    display: block; 
 
    max-height: 200px; 
 
} 
 

 
.submenu a { 
 
    background-color: #999; 
 
} 
 

 

 
.submenu a:hover { 
 
    background-color: #666; 
 
} 
 

 

 
.submenu { 
 
    overflow: hidden; 
 
    max-height: 0; 
 
    -webkit-transition: all 0.5s ease-out; 
 
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet"/> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table class="table-responsive table" id="table-diensten"> 
 

 
    <thead> 
 
     <tr> 
 
      <th>Time</th> 
 
      <th>Service</th> 
 
      <th>amount</th> 
 
      <th>total</th> 
 
      <th>BTW</th> 
 
     </tr> 
 
    </thead> 
 
    <tbody class="table-body"> 
 
     <tr class="table-row"> 
 
      <td><input type="text" class="form-control" placeholder="time (in hours)" id="time" onchange="totalofferte()"></td> 
 
      <td><input type="text" class="form-control" placeholder="service"></td> 
 
      <td><input type="text" class="form-control" placeholder="Cost (per hour)" id="cost" onchange="totalofferte()"></td> 
 
      <td>&euro; <span id="total">0,00</span></td> 
 
      <td>21%</td> 
 
     </tr> 
 
    </tbody> 
 
    <tfoot> 
 
     <tr> 
 
      <td> </td> 
 
      <td> </td> 
 
      <td><strong>Subtotaal</strong></td> 
 
      <td>&euro; <span id="subtotal">0,00</span></td> 
 
      <td> </td> 
 
     </tr> 
 
     <tr> 
 
      <td> </td> 
 
      <td> </td> 
 
      <td>21% BTW</td> 
 
      <td>&euro; <span id="costbtw">0,00</span></td> 
 
      <td> </td> 
 
     </tr> 
 
     <tr> 
 
      <td> </td> 
 
      <td> </td> 
 
      <td class="table-total"><span class="total">Totaal</span></td> 
 
      <td class="table-total"><span class="total">&euro; <span id="totalofferte">0,00</span></span></td> 
 
      <td> </td> 
 
     </tr> 
 
    </tfoot> 
 

 

 
</table> 
 

 
<a href="#table-diensten" class="add-tablerow btn btn-default" onclick="addNewRow()">add new service</a>

行爲
+1

當然爲什麼不= =)很好的解決方案 – MKAD

+1

不錯,現在我必須編寫一個代碼來計算每個服務,並將所有相加的小計:) – Gijsberts

0

它很簡單。弗里斯特創建你的元素例如:

var input = $("<input/>").attr({ 
 
    name: 'EmailSend', 
 
    type: 'text', 
 
    value: true, 
 
    class: "YOURClass" 
 
    id: "YouRid" 
 
});

比你裝箱的輸入附加到你的願望元素。例如:$(「.table-body」)。追加(輸入)

+0

這可以用於超過1個添加的輸入嗎?我想不是因爲類和ID是硬編碼的權利? – Gijsberts

+0

當然,你可以..你必須使用循環和設置ID +1 – MKAD

0

通過JavaScript

添加在DOM項目爲了與本地JavaScript您的表中添加新的項目,你將不得不使用以下

如果你想使用jQuery來代替,那麼你可以嘗試

添加自定義屬性DOM項目

如果你想添加一個新的DOM元素上的屬性,您可以使用Native JavaScript使用

或使用jQuery

循環每一次的項目

現在爲了 「處理」 這些新的價值觀,你需要遍歷所有的輸入並進行計算。迭代可以在原生Javascript中通過使用

,或者如果你想使用jQuery你可以用

相關問題