找到多个重复值时只添加一行javascript

时间:2018-03-13 12:03:44

标签: javascript jquery

如果找到多个重复值时,如何仅添加一行。这样就可以添加行 找到 accountNumber == accountNumberInTable 的数量。我添加一条新记录 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;
});

我如何只要求确认一次 不论重复记录的数量。请帮助。

1 个答案:

答案 0 :(得分:1)

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

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

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

回答你的问题:你需要在第一次重复确认后完成循环。为此目的使用return false;。它等同于for循环中的break

修改:尝试修改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;
    }
  });