bootstrapValidator表单基本使用(学习篇)
1、首先引入相关css、js文件
1 <link rel="stylesheet" type="text/css" href="../css/bootstrapValidator.min.css" /> 2 <script src="../js/bootstrapValidator.min.js" type="text/javascript" charset="utf-8"></script>
2、html部分
1 <form class="form-horizontal" role="form" id="form-registered"> 2 //文件上传 3 <div class="form-group" style="display: none;" id="operator01"> 4 <label for="managerPath" class="col-sm-5 control-label">帅哥照片</label> 5 <div class="col-sm-7" style="padding-left: 7px;"> 6 <input type="file" class="form-control" id="managerPath" name="managerPath"> 7 </div> 8 </div> 9 //输入框 10 <div class="form-group" style="display: none;" id="operator03"> 11 <label for="managerName" class="col-sm-5 control-label">帅哥姓名</label> 12 <div class="col-sm-7" style="padding-left: 7px;"> 13 <input type="text" class="form-control" name="managerName" id="managerName" placeholder="经办人姓名"> 14 </div> 15 </div> 16 //下拉选择器 17 <div class="form-group"> 18 <label for="industryType" class="col-sm-5 control-label">帅哥类别</label> 19 <div class="col-sm-7" style="padding-left: 7px;"> 20 <select class="form-control" name="industryType" id="industryType"> 21 <option>选择行业类别</option> 22 <option value="0">IT</option> 23 <option value="1">金融</option> 24 <option value="2">空乘</option> 25 </select> 26 </div> 27 </div> 28 //日期时间 29 <div class="form-group"> 30 <label class="col-sm-5 control-label" for="establishDate">帅哥日期:</label> 31 <div class='col-sm-7' id='datetimepicker1' style="padding-left: 7px;"> 32 <input type='date' name="establishDate" id="establishDate" class="form-control" /> 33 </div> 34 </div> 35 //文本域 36 <div class="form-group"> 37 <label for="desc" class="col-sm-5 control-label">机构简介</label> 38 <div class="col-sm-7" style="padding-left: 7px;"> 39 <textarea class="form-control" name="desc" id="desc" rows="3"></textarea> 40 </div> 41 </div> 42 单选按钮 43 <div class="form-group"> 44 <label for="bankCardType" class="col-sm-5 control-label">银行卡类型</label> 45 <div class="col-sm-7" style="padding-left: 7px;"> 46 <input type="radio" style="margin-left: 5px;" name="radio1" value="借记卡" /> 借记卡 47 <input type="radio" style="margin-left: 45px;" name="radio1" value="单位结算卡" /> 单位结算卡 48 </div> 49 </div> 50 <div class="form-group"> 51 <label for="name" class="col-sm-5 control-label"></label> 52 <div class="col-sm-7" style="padding-left: 7px;"> 53 <button onclick="handelSubmit()" type="submit" name="submit" class="btn btn-default">提交认证</button> 54 </div> 55 </div> 56 </form>
3、js部分
1 window.onload = function(){ 2 $('#form-registered').bootstrapValidator({ 3 message: 'This value is not valid', 4 excluded: [':disabled'], 5 feedbackIcons: { 6 valid: 'glyphicon glyphicon-ok', 7 invalid: 'glyphicon glyphicon-remove', 8 validating: 'glyphicon glyphicon-refresh' 9 }, 10 fields: { 11 managerPath: { 12 validators: { 13 notEmpty: { 14 message: '帅哥照片不能为空' 15 }, 16 }, 17 }, 18 managerName: { 19 validators: { 20 notEmpty: { 21 message: '帅哥名称不能为空' 22 }, 23 }, 24 }, 25 industryType: { 26 validators: { 27 notEmpty: { 28 message: '帅哥类别不能为空' 29 }, 30 callback: { 31 message: '选择行业类别不能为空', 32 callback: function(value, validator) { 33 if (value == "选择行业类别") { 34 return false; 35 } else { 36 return true; 37 } 38 } 39 } 40 }, 41 }, 42 establishDate: { 43 validators: { 44 notEmpty: { 45 message: '帅哥日期不能为空' 46 }, 47 }, 48 }, 49 desc: { 50 validators: { 51 notEmpty: { 52 message: '机构简介不能为空' 53 }, 54 }, 55 }, 56 desc: { 57 validators: { 58 notEmpty: { 59 message: '机构简介不能为空' 60 }, 61 }, 62 }, 63 radio1: { 64 validators: { 65 notEmpty: { 66 message: '银行卡类型不能为空' 67 }, 68 }, 69 }, 70 }) 71 } 72 //表单提交 73 function handelSubmit() { 74 //先校验,在调是否通过校验的方法 75 $("#form-registered").data('bootstrapValidator').validate(); 76 //校验表单是否通过 77 var flag = $("#form-registered").data('bootstrapValidator').isValid(); 78 if (flag) { 79 //获取要提交的值 80 let managerName = $('#managerName').val(); 81 //发送请求 82 $.ajax({ 83 type: "PUT", //请求方式 84 url: register + "v1/enterprise-user", //地址,就是json文件的请求路径 85 dataType: "json", //数据类型可以为 text xml json script jsonp 86 contentType: "application/json", 87 data: managerName, 88 success: function(res) { //返回的参数就是 action里面所有的有get和set方法的参数 89 //请求成功后要做的事情 90 } 91 }); 92 } 93 }
转自:https://blog.csdn.net/skyblue_afan/article/details/123328743