bootstrapValidator表單校驗、更改狀態(tài)、新增、移除校驗字段的實例代碼
注意:jQuery報 Maximum call stack size exceeded
錯誤描述:超出最大調(diào)用堆棧大小
錯誤原因:內(nèi)部形成遞歸
解決方案: html 寫法不規(guī)范,更改 html 寫法,可參考官網(wǎng)示例,或下面代碼。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>validate</title>
<link rel="stylesheet">
<link rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row">
<form id="inputForm">
<div class="form-group">
<label class="col-lg-4 control-label">姓名</label>
<div class="col-lg-8">
<input type="text" class="form-control" name="xm" />
</div>
</div>
<div class="form-group">
<label class="col-lg-4 control-label">性別</label>
<div class="col-lg-8">
<input type="text" class="form-control" name="xb" />
</div>
</div>
<div class="form-group">
<label class="col-lg-4 control-label">年齡</label>
<div class="col-lg-8">
<input type="text" class="form-control" name="nl" />
</div>
</div>
</form>
<div class="row">
<button id="btn1" class="btn btn-primary" >提交</button>
<button id="btn2" class="btn btn-primary" >更改校驗狀態(tài)</button>
<button id="btn3" class="btn btn-primary" >增加校驗字段</button>
<button id="btn4" class="btn btn-primary" >移除校驗字段</button>
<button id="btn5" class="btn btn-primary" >重置表單校驗</button>
</div>
</div>
</div>
</body>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/2.2.4/jquery.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/js/bootstrap.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/bootstrap-validator/0.5.3/js/bootstrapValidator.js"></script>
<script>
$(function(){
var formValidator = function(){
$('#inputForm').bootstrapValidator({
fields:{
xm:{
validators:{
notEmpty:{
message: '姓名不能為空'
}
}
},
xb:{
validators:{
notEmpty:{
message: '性別不能為空'
}
}
}
}
});
}
formValidator();
// 提交表單
$('#btn1').on('click', function(){
var bootstrapValidator = $('#inputForm').data('bootstrapValidator');
bootstrapValidator.validate();
if(bootstrapValidator.isValid()){
alter('提交成功');
}
});
// 改變字段校驗狀態(tài)
$('#btn2').on('click', function(){
/*
NOT_VALIDATED 未校驗的
VALIDATING 校驗中的
INVALID 校驗失敗的
VALID 校驗成功的
*/
// $('#inputForm').bootstrapValidator('updateStatus', 'xm', 'NOT_VALIDATED');
// 或
$('#inputForm').data('bootstrapValidator').updateStatus('xm', 'NOT_VALIDATED');
});
// 增加校驗字段
$('#btn3').on('click', function(){
/*$('#inputForm').bootstrapValidator('addField', 'nl', {
validators:{
notEmpty:{
message: '年齡不能為空'
}
}
});*/
// 或
$('#inputForm').data('bootstrapValidator').addField('nl', {
validators:{
notEmpty:{
message: '年齡不能為空'
}
}
});
});
// 移除校驗字段
$('#btn4').on('click', function(){
// 字段可以處于校驗中,所以先處理校驗狀態(tài)
// $('#inputForm').bootstrapValidator('updateStatus', 'xm', 'NOT_VALIDATED');
// $('#inputForm').bootstrapValidator('removeField', 'xm');
// 或
$('#inputForm').data('bootstrapValidator').updateStatus('xm', 'NOT_VALIDATED');
$('#inputForm').data('bootstrapValidator').removeField('xm');
});
// 重置表單校驗
$('#btn5').on('click', function(){
$('#inputForm').bootstrapValidator('resetForm', true);
// 或
//$('#inputForm').data('bootstrapValidator').resetForm(true);
});
});
</script>
</html>
總結(jié)
到此這篇關(guān)于bootstrapValidator表單校驗、更改狀態(tài)、新增、移除校驗字段的實例代碼的文章就介紹到這了,更多相關(guān)bootstrapvalidator表單校驗內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
jQuery EasyUI常用數(shù)據(jù)驗證匯總
這篇文章主要為大家詳細匯總了jQuery EasyUI常用數(shù)據(jù)驗證,介紹了validatebox()提供的自定義驗證,感興趣的小伙伴們可以參考一下2016-09-09
jQuery+easyui中的combobox實現(xiàn)下拉框特效
這篇文章主要介紹了jQuery+easyui中的combobox實現(xiàn)下拉框特效的幾種方法,非常簡單實用,需要的朋友可以參考下2015-02-02
jQuery插件echarts實現(xiàn)的循環(huán)生成圖效果示例【附demo源碼下載】
這篇文章主要介紹了jQuery插件echarts實現(xiàn)的循環(huán)生成圖效果,結(jié)合完整實例形式分析了echarts插件循環(huán)輸出數(shù)字圖形的實現(xiàn)步驟與相關(guān)操作技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2017-03-03

