Twitter bootstrap 模态不重置窗体的引导验证程序
我在模态内部为我的表单使用引导验证程序。该验证程序在后续表单访问中被保留。例如,如果执行以下操作Twitter bootstrap 模态不重置窗体的引导验证程序,twitter-bootstrap,bootstrap-modal,jqbootstrapvalidation,Twitter Bootstrap,Bootstrap Modal,Jqbootstrapvalidation,我在模态内部为我的表单使用引导验证程序。该验证程序在后续表单访问中被保留。例如,如果执行以下操作 Access表单输入一些有效值并提交表单 再次访问表单该字段的颜色仍为绿色,因为我在上次提交表单时输入了有效条目 <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="modal-
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<h4 class="modal-title" id="myModalLabel">Add new Node</h4>
</div>
<div class="modal-body">
<div class="modal-body">
<form role="form" id="addNodeForm">
<div class="form-group">
<label for="nodename" class="control-label">Node Name</label>
<input type="text" name="nodename" class="form-control" id="nodename" required>
</div>
</form>
</div>
<!-- <div class="alert alert-success">
<a href="#" class="close" data-dismiss="alert">×</a>
<div id="thanks"></div>
</div> -->
</div>
<div class="modal-footer">
<button type="button" id="add-close" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" id="add-save" class="btn btn-primary">Save changes</button>
</div>
</div>
hidden.bs.modal
事件在模态对用户隐藏完成时触发,因此表单将被隐藏,BoostarpValidator默认情况下不会处理或验证隐藏字段
因此,为了在隐藏模式时重置表单,您应该添加:
excluded: ':disabled'
你的JS代码
实例:
参考:
单据:排除的
excluded
选项后,表单上bootstrapValidator的初始化应如下所示:
$('#addNodeForm').bootstrapValidator({
feedbackIcons: {
valid: 'glyphicon glyphicon-ok',
invalid: 'glyphicon glyphicon-remove',
validating: 'glyphicon glyphicon-refresh'
},
excluded: ':disabled', // <=== Adding the 'excluded' option
fields: {
nodename: {
validators: {
notEmpty: {
message: 'Function name is required'
}
}
}
}
});
$('addNodeForm')。bootstrapValidator({
反馈图标:{
有效:“glyphicon glyphicon ok”,
无效:“glyphicon glyphicon删除”,
正在验证:“glyphicon glyphicon刷新”
},
已排除:':disabled',//请尝试以下代码:
$('#contactform').bootstrapValidator('resetForm', true);
请尝试以下代码,在哪个变电站重置表单
$('#addNodeForm').trigger("reset");
在模态即将隐藏时重置窗体
$("#addUserModal").on('hide.bs.modal', function(){
$("#userForm")[0].reset();
$('#userForm').bootstrapValidator('resetForm', true);
});
reset()函数将重置表单非引导验证错误
因此,我们需要做的只是告诉表单您的表单是新的,从表单中删除已验证。如下所示
document.getElementsByClassName('needs-validation')[0].classList.remove("was-validated")
我尝试添加,但我仍然可以看到表单具有旧的验证。字段仍然以绿色显示在屏幕上reopen@SowmyaGanesan您是否尝试过我提供给您的实时示例?而且,您必须仔细阅读我示例的源代码
document.getElementsByClassName('needs-validation')[0].classList.remove("was-validated")