gpt4 book ai didi

javascript - 当发现有多个重复值时,仅添加一次行 javascript

转载 作者:行者123 更新时间:2023-12-03 02:18:44 24 4
gpt4 key购买 nike

当发现有多个重复值时,如何仅添加一次行。这里根据到找到的 accountNumber == accountNumberInTable 的数量。我用 say 添加一条新记录NS-01,当我添加具有相同记录 NS-01 的另一个条目时,它要求进行一次确认。同样,如果我添加具有相同 NS-01 值的记录,它会要求确认两次,因为有两行具有相同的记录。

$('#AddNewRowButton').click(function () {
debugger;
var index = $('#CollectionTable tbody tr').length;
var imageButton = "<button type='button' style='background-color:transparent; margin-top:-4px;' class='btn btn-flat' id='DeleteImageButton' onmouseover=this.style.cursor='pointer'><i style='font-size:11px;' class='glyphicon glyphicon-trash'></i></button>";
var statementReference = $('#StatementReferenceTextBox').val();
var accountNumberId = $("#AccountNumberIdHiddenField").val();
var accountNumber = $('#AccountNumberTextBox').val();
var customerId = $('#CustomerIdTextBox').val();
var name = $('#CustomerNameTextBox').val();
var unit = $('#UnitTextBox').val();
var collectorName = $('#CollectorsSelect :selected').text();;
var productName = $('#ProductTextBox').val();
var amount = parseFloat($('#AmountTextBox').val());
if (isNaN(amount)) {
amount = 0;
}

var amountTextBox = "<input type='text' id='TableAmountTextBox'" + "value=" + amount + " style='text-align:right;' />";

if (amount == '' || accountNumber == '' || customerId == '') {
$('#DialogDiv').empty();
$('#DialogDiv').append('Cannot add empty values');
$('#DialogDiv').slideDown(200);
return;

}


var newRow = "<tr><td>" + imageButton + "</td><td>" + (index + 1) + "</td><td>" + statementReference +
"</td><td style='display:none;'>" + accountNumberId + "</td><td>" + accountNumber + "</td><td>" + customerId + "</td><td>" + name + "</td><td>" + unit + "</td><td>" + collectorName + "</td><td>" + productName + "</td><td>" + amountTextBox + "</td></tr>";
var alreadyExists = false;
var i = collectionSheetDetails.length;
$('#CollectionTable > tbody > tr').each(function () {
var accountNumberInTable = $(this).find('td:eq(4)').text();
if (accountNumber == accountNumberInTable) {
var r = confirm('There is already an entry with the same account number. Proceed anyway?');
if (r == true) {
alreadyExists = true;
addNewRow(newRow);
calculateTotal();
clearReadOnlyFields();
if ($('#AccountNumberTextBox').prop('disabled') == false) {
$('#AccountNumberTextBox').focus();
}
enableDisableCollector();

} else {
alreadyExists = true;
if ($('#AccountNumberTextBox').prop('disabled') == false) {
$('#AccountNumberTextBox').focus();
clearReadOnlyFields();
$('#AccountNumberTextBox').val('');

};
}
}


});
if (!alreadyExists) {
addNewRow(newRow);
calculateTotal();
clearReadOnlyFields();
if ($('#AccountNumberTextBox').prop('disabled') == false) {
$('#AccountNumberTextBox').focus();
}
enableDisableCollector();
}

if ($('#NumberTextBox').prop('disabled') == false) {
$('#NumberTextBox').focus();
}
if ($('#AccountNumberTextBox').prop('disabled') != true) {
$('#AccountNumberTextBox').focus();
}
var colDiv = document.getElementById("CollectionSheetTable");
console.log(colDiv);
colDiv.scrollTop = colDiv.scrollHeight;

//$("#CollectionTable").animate({ scrollTop: $(document).height() }, "slow");
return false;
});

如何让它只要求确认一次不管有多少重复记录。请帮忙。

最佳答案

首先,这是一个无用的检查:

if ($('#CollectionTable > tbody > tr').length > 0)

你在$.each内执行此操作循环相同的选择器,因此如果循环正在运行,您就已经知道它存在。另外,这将始终为您提供第一个找到的长度 <tr> .

回答您的问题:您只需在第一次重复确认后完成循环即可。使用return false;以此目的。相当于break在 for 循环中。

编辑:尝试修改您的 each像这样:

$('#CollectionTable > tbody > tr').each(function () {      
var accountNumberInTable = $(this).find('td:eq(4)').text();
if (accountNumber == accountNumberInTable) {
var r = confirm('There is already an entry with the same account number. Proceed anyway?');
if (r == true) {
alreadyExists = true;
addNewRow(newRow);
calculateTotal();
clearReadOnlyFields();
if ($('#AccountNumberTextBox').prop('disabled') == false) {
$('#AccountNumberTextBox').focus();
}
enableDisableCollector();
} else {
alreadyExists = true;
if ($('#AccountNumberTextBox').prop('disabled') == false) {
$('#AccountNumberTextBox').focus();
clearReadOnlyFields();
$('#AccountNumberTextBox').val('');
};
}
return false;
}
});

关于javascript - 当发现有多个重复值时,仅添加一次行 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49255712/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com