在下面的代码片段中,我想如果用户添加了更多行,那么也应该在其上实现验证,并且没有两个选择的业务框应该具有相同的值。
两个选择框的名称都是自解释的,因为我需要将所有值传递给下一页。
$(document).ready(function() {
$(".addmore").on('click',function(e){
var newrow=$('#businessgroup .form-group:first').clone();
$('#businessgroup').append(newrow);
});
$("#addUserForm").validate({
rules: {
'business_unit[]': {
required: true,
},
'department[]': {
required: true,
},
},
messages: {
'business_unit[]': {
required: "Please select business unit."
},
'department[]': {
required: "Please select department."
},
}
});
});
<link href="http://112.196.27.245/bnp/public/css/style.default.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://112.196.27.245/bnp/public/js/jquery.validate.js"></script>
<form id="addUserForm" name="addUserForm">
<div id="businessgroup">
<div class="form-group">
<select name="business_unit[]">
<option value="">Select Business Unit</option>
<option value="17">Business Unit 1</option><option value="18">Business Unit 2</option><option value="19">Business Unit 3</option>
</select>
<select name="department[]">
<option value="">Select Department</option>
<option value="17">Business Unit 1</option><option value="18">Business Unit 2</option><option value="19">Business Unit 3</option>
</select>
</div>
<div class="col-sm-3"><button type="button" class="btn btn-primary addmore">Add More</button></div>
</div>
<input type="submit" name="addUserBtn" value="Add" class="btn btn-primary">
</form>