【发布时间】:2019-12-07 11:33:48
【问题描述】:
我试图验证动态添加的行,但有些故障它不起作用。 我找不到我的问题。 任何人都告诉我或纠正我为什么它不能正常工作..
我需要验证动态添加行和列。
这是我的完整代码..
这是我的示例代码..
$(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>
我需要验证动态添加行和列。
谢谢
【问题讨论】:
-
我没有看到表单元素,jquery validate 有必要吗?
-
@ArunmainthanKamalanathan 是的,先生!对不起,我忘记了..请检查我更新的问题..
-
我成功了。重要的一点是使名称值唯一。
-
@suguspnk 第一行验证只能工作兄弟..我使用类和每个函数..还有其他方法吗?
-
你更新了什么?如果您解决了获取计数器的问题,然后将该计数器连接到名称,那么它将正常工作。
标签: javascript jquery html jquery-validate