动态行验证不起作用 - jquery?

Dynamically row validation not working - jquery?

我试图验证动态添加的行,但有些错误无法正常工作。 我找不到我的问题。 任何人告诉我或纠正我为什么它不能正常工作..

我需要验证动态添加行和列。

这是我的完整代码..

Full code Fiddle <3

这是我的示例代码..

$(document).ready(function() {
  $("#ok_button").on("click", function() {

    $(".act").val($("#cash_text:first").val());
  });
  $("#add_Row").on("click", function() {

    var counter = 0;
    var idVal = $('#tab_logic tr:last').find('td:first').html();
    // alert(idVal);
    if (idVal === undefined) {
      counter = 0;
    } else {
      var matches = idVal.match(/\d+/g);
      // alert(matches);
      if (matches != null) {
        counter = Number(matches) + counter + 1;
      }
    }
    var newRow = $("<tr>");
    var cols = "";
    if ($('.price').length > 0) {
      var first_ele = document.querySelector('.price').value;
    } else {
      var first_ele = '';
    }
    cols += '<td><select class="form-control sel_sel status required" id="accountName' + counter + '" name="accountName" for="accountName" required><option value="">Choose an account</option><option value="1">code 1</option></select></td>';
    cols += '<td><input value="' + first_ele + '" type="text" class="form-control required price narr" name="narr" placeholder="Enter your text here" id="acc_narrat' + counter + '" data-toggle="modal" data-target="#narratModal"/></td>';

    cols += '<td><input type="number" class="form-control sumTot deb allignAmt" id="cashdeb' + counter + '" min="0" data-action="sumDebit" name="debit" placeholder="Debit amount" onkeypress="restrictMinus(event);" required/></td>';

    cols += '<td style="width: 11%;"><button type="button" class="adRow ibtnDel" style="width:30%;position: relative;right: 25%;">x</button></a></td>'

    newRow.append(cols);


    var defVal = $("select[name=acctname]").find(":selected").val();
    if (defVal) {
      $("select[name=accountName]").find(`option[value=${defVal}]`).hide();
    }
    $("table.order-list").append(newRow);
    setValCashVal('accountName'.concat(counter));
    bindScript();
    counter++;

  });
  $("table.order-list").on("click", ".ibtnDel", function(_event) {
    $(this).closest("tr").remove();
    if ($("#tab_logic tbody tr").length == 1)
      $("#cash_text:first").val('');
    evaluateTotal();

  });
});


// dynamic row validations

$(document).ready(function() {

  $('#contactForm').on('submit', function(event) {
    console.log($('.narr'))
    $('.status').each(function() {
      $(this).rules("add", {
        required: true
      })
    });
    $('.narr').each(function() {
      $(this).rules("add", {
        required: true
      })
    });
    $('.deb').each(function() {
      $(this).rules("add", {
        required: true
      })
    });
    event.preventDefault();
    if ($('#contactForm').validate().form()) {
      alert("validates");
    } else {
      alert("does not validate");
    }
  });
  $('#contactForm').validate();
});
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<!-- validation cdn -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery.bootstrapvalidator/0.5.2/css/bootstrapValidator.min.css">
<script src="https://ajax.aspnetcdn.com/ajax/jquery.validate/1.19.0/jquery.validate.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.bootstrapvalidator/0.5.2/js/bootstrapValidator.min.js"></script>

<form id="contactForm">
  <input type="button" class="add_Row adRow" id="add_Row" value="Add Row">
  <table class="table table-bordered table-hover order-list" id="tab_logic" style="width:100% !important">
    <thead>
      <tr style="background-color: #680779; color: #fff;">
        <th class="text-center">
          select code*
        </th>
        <th class="text-center">
          description*
        </th>
        <th class="text-center">
          amount*
        </th>
        <th class="text-center">
          action*
        </th>
      </tr>
    </thead>
  </table>

  <!-- submit button -->

  <div class="form-group col-4 vocSub" style="margin-bottom: 0px !important;">
    <div class="col-md-12 cashform_submit" id="">
      <input type="submit" class="btn add-btn submit-btn load cashmainBtn" id="cashSub" value="Submit" />
    </div>
  </div>

</form>

我需要验证动态添加行和列。

谢谢

关于更改的要点优先:

  • 递增计数器的实现出现问题,所以我先将其更改为工作计数器。问题是您在后续行中获得计数器的 NaN 值,因此您不会获得后续行的唯一名称。我刚刚在外部创建了一个变量,每次单击添加按钮时该变量都会递增。
  • 我只是在展示如何让它发挥作用。对我来说,它如何处理错误消息显示的框架存在问题。我的意思是,输入字段的名称应该是唯一的,以便显示错误消息。这会给数组类型的字段带来问题,就像您在后端遇到的问题一样。我想你将不得不在后端即兴发挥。

这里是总结的代码片段:

let counter = 0;

$(document).ready(function() {
  $("#add_Row").on("click", function() {
    counter++;

    ...

    cols += '<td><select class="form-control sel_sel status required" id="accountName' + counter + '" name="accountName'+counter+'" for="accountName" required><option value="">Choose an account</option><option value="1">code 1</option></select></td>';
    cols += '<td><input value="' + first_ele + '" type="text" class="form-control required price narr" name="narr'+counter+'" placeholder="Enter your text here" id="acc_narrat' + counter + '" data-toggle="modal" data-target="#narratModal"/></td>';

    cols += '<td><input type="number" class="form-control sumTot deb allignAmt" id="cashdeb' + counter + '" min="0" data-action="sumDebit" name="debit'+counter+'" placeholder="Debit amount" onkeypress="restrictMinus(event);" required/></td>';

    ...

  });

  ...

});

...

请注意动态字段名称属性中的串联计数器。