[英]Array output for each table cell
This is a table for user to record Expenses and Amount.这是一个供用户记录费用和金额的表格。 User can add table cell to record more items with the amount.
用户可以添加表格单元格以记录更多带有金额的项目。 After user key in the Amount, it will show 7% tax at beside and total amount at the top.
用户输入金额后,旁边会显示7%的税,顶部会显示总金额。 But now i am facing 2 problems.
但现在我面临两个问题。
If user added 2 items, and input the 2 items' amount, then if they deleted the row, the total amount will not deduct after the row is deleted.如果用户添加了 2 个项目,并输入了 2 个项目的金额,那么如果他们删除了该行,则该行删除后总金额不会扣除。
the tax will only added in first row.税收只会添加在第一行。 I want to do it in each row with 7% tax with each item's amount.
我想在每一行中对每个项目的金额征收 7% 的税。
So can i know how to solve this 2 problems?那么我能知道如何解决这两个问题吗?
function validate(evt) { var theEvent = evt || window.event; var key = theEvent.keyCode || theEvent.which; key = String.fromCharCode(key); var regex = /[0-9]|\\./; if (!regex.test(key)) { theEvent.returnValue = false; if (theEvent.preventDefault) theEvent.preventDefault(); } } function force2decimals(event) { var value = $(event).val(); var format_val = parseFloat(value).toFixed(2); $(event).val(format_val); } //<----------------------2 Decimal force END--------------------> // + function mFunction() { document.getElementById("rowrow").insertRow(-1).innerHTML = '<tr><td></td><td><output id="gst">0.00</output></td><td><input type="text" name="Amount[]" id="columninput" class="input" oninput="myFunction(this.value)" placeholder="Amount" style="font-size:14px;" min="0" lang="en-150" onchange="force2decimals(this)" onkeypress="validate(event)" inputmode="numeric"></td></tr>'; } // - function remove() { var x = document.getElementById("rowrow").rows.length; if (x == 1) {} else { document.getElementById("rowrow").deleteRow(-1); }; } function myFunction() { const ele = document.querySelectorAll('input.input'); let sum = 0; ele.forEach(input => { sum += input.value ? parseFloat(input.value) : 0; }); document.getElementById('result').textContent = sum.toFixed(2); document.getElementById('gst').textContent = (sum * 0.07).toFixed(2); }
.css-serial { counter-reset: serial-number; } .css-serial td:first-child:before { counter-increment: serial-number; content: counter(serial-number); }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <script src="http://code.jquery.com/jquery-1.10.2.min.js"></script> <div id="container"> <div id="body"> <br> <div class="row"> <div class="css-serial" style="overflow-x:auto;"> <table id="rowrow"> <tr> <td id="number"></td> <td><output id="gst">0.00</output></td> <td><input type="text" name="Amount[]" class="input" oninput="myFunction(this.value)" id="columninput" placeholder="Amount" style="font-size:14px;" min="0" lang="en-150" onchange="force2decimals(this)" onkeypress='validate(event)' inputmode='numeric' required></td> Total Amount <output id="result"> 0.00 </output> <input type="button" id="slipbutton1" onclick="mFunction();" name='add' value="+" /> <input type="button" id="slipbutton2" onclick="remove();" name='remove' value="-" /><br><br> </table> </div> </div> <br> </div> </div>
I have managed to make changes as per your need, also I have used jquery
as you had added into your code.我设法根据您的需要进行了更改,我也使用了您添加到代码中的
jquery
。
I have calculated output in the remove()
you can move it to a common function to avoid dirty code.我已经计算了
remove()
中的输出,您可以将其移动到一个通用函数中以避免脏代码。
function remove() {
var x = document.getElementById("rowrow").rows.length;
if (x == 1) {} else {
var ele = document.querySelectorAll('input.input');
let sum = 0;
ele.forEach(input => {
sum += input.value ? parseFloat(input.value) : 0;
});
sum = sum - ele[ele.length - 1].value;
document.getElementById('result').textContent = sum.toFixed(2);
document.getElementById("rowrow").deleteRow(-1);
};
}
Changed GST id to class, as id should be unique.将 GST id 更改为 class,因为 id 应该是唯一的。
Added this line of code to print GST into each row.添加了这行代码以将 GST 打印到每一行。
$(input).parents("tr").find(".gst").text((input.value * 0.07).toFixed(2));
you had added 2 class attributes to input in JS template, also merged them.您在 JS 模板中添加了 2 个类属性以输入,也合并了它们。
<tr>
<td></td>
<td><output class="gst">0.00</output></td>
<td>
<input
type="text"
name="Amount[]"
class="columninput input" // This line
oninput="myFunction(this.value)"
placeholder="Amount"
style="font-size:14px;"
min="0" lang="en-150"
onchange="force2decimals(this)"
onkeypress="validate(event)"
inputmode="numeric"
>
</td>
</tr>
function validate(evt) { var theEvent = evt || window.event; var key = theEvent.keyCode || theEvent.which; key = String.fromCharCode(key); var regex = /[0-9]|\\./; if (!regex.test(key)) { theEvent.returnValue = false; if (theEvent.preventDefault) theEvent.preventDefault(); } } function force2decimals(event) { var value = $(event).val(); var format_val = parseFloat(value).toFixed(2); $(event).val(format_val); } //<----------------------2 Decimal force END--------------------> // + function mFunction() { document.getElementById("rowrow").insertRow(-1).innerHTML = '<tr><td></td><td><output class="gst">0.00</output></td><td><input type="text" name="Amount[]" class="columninput input" oninput="myFunction(this.value)" placeholder="Amount" style="font-size:14px;" min="0" lang="en-150" onchange="force2decimals(this)" onkeypress="validate(event)" inputmode="numeric"></td></tr>'; } // - function remove() { var x = document.getElementById("rowrow").rows.length; if (x == 1) {} else { var ele = document.querySelectorAll('input.input'); let sum = 0; ele.forEach(input => { sum += input.value ? parseFloat(input.value) : 0; }); sum = sum - ele[ele.length - 1].value; document.getElementById('result').textContent = sum.toFixed(2); document.getElementById("rowrow").deleteRow(-1); }; } function myFunction() { debugger var ele = document.querySelectorAll('input.input'); let sum = 0; ele.forEach(input => { sum += input.value ? parseFloat(input.value) : 0; $(input).parents("tr").find(".gst").text((input.value * 0.07).toFixed(2)); }); document.getElementById('result').textContent = sum.toFixed(2); //document.getElementById('gst').textContent = (sum * 0.07).toFixed(2); }
.css-serial { counter-reset: serial-number; } .css-serial td:first-child:before { counter-increment: serial-number; content: counter(serial-number); }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="container"> <div id="body"> <br> <div class="row"> <div class="css-serial" style="overflow-x:auto;"> <table id="rowrow"> <tr> <td id="number"></td> <td><output class="gst">0.00</output></td> <td><input type="text" name="Amount[]" class="input" oninput="myFunction(this.value)" id="columninput" placeholder="Amount" style="font-size:14px;" min="0" lang="en-150" onchange="force2decimals(this)" onkeypress='validate(event)' inputmode='numeric' required></td> Total Amount <output id="result"> 0.00 </output> <input type="button" id="slipbutton1" onclick="mFunction();" name='add' value="+" /> <input type="button" id="slipbutton2" onclick="remove();" name='remove' value="-" /><br><br> </table> </div> </div> <br> </div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.