【发布时间】:2021-02-26 03:53:23
【问题描述】:
循环时,我想对借方和贷方金额进行验证。当用户在借方列输入时,输入类型将为“10”,当用户在贷方列输入时,输入类型将为“20”,但用户不能在同一行输入借方和贷方金额。最后,如果借方和贷方金额不相符,它将无法将数据传递给ajax。我不确定如何对数组进行验证。有人有更适合我的情况的想法或任何其他建议吗?
此外,如何将序列号添加到动态行并插入数组中以便可以将其保存到数据库中? JSFiddle
Example input:
|Description|Debit|Credit|
| Item A | 500 | | // scenario 1
| Supplier | | 500 |
| Item B | 300 | | //scenario 2
| Item C | 300 | |
| Supplier | | 600 |
| Total | 1100| 1100 |
因为会计他们可能会输入 1 借 1 贷或 2 借 1 贷,但双方的金额是一致的。所以我做了一个场景1和场景2。
以下是我的编码,我不确定如何对此进行验证。请检查我的代码
<table id="example-input" class="table table-bordered text-nowrap">
<thead>
<tr>
<th class="wd-5p"></th>
<th class=" wd-20p">Description</th>
<th class="wd-10p tx-right">Debit</th>
<th class="wd-10p tx-right">Credit</th>
</tr>
</thead>
<tbody class="acc_table">
<tr class="acc-row">
<td>
<a id="delete" href="javascript:;" title="Remove row">Delete</i></a></td>
<td>
<input class="form-control tx-right row-accDesc" type="text"></td>
<td>
<input class="form-control tx-right row-dr" type="text" value="0"></td>
<td>
<input class="form-control tx-right row-cr" type="text" value="0"></td>
</tr>
</tbody>
<tfoot>
<tr id="hiderow">
<td colspan="6" class="tx-center tx-15"><b><a id="addrow" href="javascript:;" title="Add a row"><i class="fe fe-plus-circle"></i>Add a Row</a></b></td>
<br>
</tr>
<tr>
<td class="valign-middle"></td>
<td class="tx-right">Total</td>
<td class="tx-right">
<input class="form-control tx-right" type="text" placeholder="0.00" id="debit" disabled></td>
<td class="tx-right">
<input class="form-control tx-right" type="text" placeholder="0.00" id="credit" disabled></td>
</tr>
</tfoot>
</table>
<script>
$(document).ready(function () {
AddAccount();
$('#addrow').click(function () {
addRow();
});
});
$(document).on('click', '.delete-row', function () {
$(this).parents('.acc-row').remove();
});
$(document).on('keyup', '.row-dr, .row-cr', function (e) {
var dr = 0;
cr = 0;
total_cr = 0,
total_dr = 0;
var $row = $(this).closest("tr"); //this is the closest common root of the input elements
dr += parseFloat($row.find('input.row-dr').val());
cr += parseFloat($row.find('input.row-cr').val());
//loop
$(".acc-row").each(function () {
total_dr += $(this).find(".row-dr").val() != "" ? parseFloat($(this).find(".row-dr").val()) : 0
total_cr += $(this).find(".row-cr").val() != "" ? parseFloat($(this).find(".row-cr").val()) : 0
})
//add result to inputs
$("#debit").val(total_dr.toFixed(2))
$("#credit").val(total_cr.toFixed(2))
});
function addRow() {
var addRows =
'<tr class="acc-row">' +
'<td><a class="delete-row" href="javascript:; ">Delete</i></a></td>' +
'<td><input class="form-control tx-right row-accDesc" type="text"></td>' +
'<td><input class="form-control tx-right row-dr" type="text"></td>' +
'<td><input class="form-control tx-right row-cr" type="text"></td>' +
'</tr>'
$(".acc_table").append(addRows);
}
function AddAccount() {
$("#btnAddAccount").click(function (e) {
var arrayDetail = [];
var sArrayDetail = "";
$.each($(".acc_table .acc-row"), function (index, value) {
//how can I add sequence number? [number of rows] 1,2,3,4...
// when looping I want to do a validation here
// if user enter debit side, the entry type will be '10',
// if user enter credit side, the entry type will be '20',
// user are not able to enter debit and credit side in a row.
let desc = $(this).find(".row-desc").val()
let debit = $(this).find(".row-dr").val()
let credit = $(this).find(".row-cr").val()
let entrytype = 0
let detail = {
accDescription: desc,
accAmount: debit,//how can I define the amount is on debit or credit side?
accEntryType: entrytype,//(debit:10, :credit 20), //at there it should give me either 10 or 20 based on where the user key in the amount.
}
arrayDetail.push(detail)
});
console.log(arrayDetail)
sArrayDetail = JSON.stringify(arrayDetail);
$.ajax({
type: "POST",
url: "AddAccount",
data: JSON.stringify({sArrayDetail }),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
},
failure: function (response) {
alert(response.d);
}
});
})
};
</script>
【问题讨论】:
-
嗨,
the entry type will be '10' and when user enter at the credit column, the entry type will be '20'..是什么意思? -
这意味着在数据库中我有一个 AccEntryType 列,它存储“10”或“20”值,因为在我的数据库中,我将 10 设置为债务人,20 作为债权人。这样我就知道金额记录在谁名下。
-
所以当用户在借方栏输入金额时,会定义该金额的输入类型为10。
标签: html jquery asp.net arrays