最近开发项目,为了页面比较简洁美观,因而引入了bootstrap,不得不说bootstrap封装了不少东西,用起来很方便,在开发过程当中,我主要应用了 模态框 和 表单验证 ,感受效果不错,特此记录备忘同时与你们分享一下。javascript
1.须要导入的文件css
<script src="jquery.min.js"></script> <script src="bootstrap.min.js"></script> <link rel="stylesheet" href="bootstrap.min.css"/>
2.在页面中,定义一个按钮来触发模态框html
## 媒体建立 模态框 start <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <form class="form-horizontal" role="form" method="post" id='formva' action = ''> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> <h4 class="modal-title" id="myModalLabel">建立媒体</h4> </div> <div class="modal-body"> <div class="form-group"> <label for="mediaTypeM" class="col-sm-2 control-label">媒体类型</label> <div class="col-sm-6"> <select class="form-control" id="mediaTypeM" name="mediaTypeM"> <option value="1">PC</option> <option value="2">WAP</option> <option value="3">APP</option> </select> </div> </div> </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button> <button type="button" class="btn btn-primary" onclick="saveAppInfo()">保存</button> </div> </div><!-- /.modal-content --> </div><!-- /.modal --> </form> </div> ## 建立的模态框 end
3.调用方法java
能够在按钮上,加上属性直接触发模态框的开启关闭jquery
<button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
也能够用onclick方法,添加一个js函数来触发bootstrap
//开启 function createAppInfo(){ $("#myModal").modal('show'); } // 关闭 $("#myModal").modal('hide');
1.须要导入的文件less
<script src="jquery.min.js"></script> <link rel="stylesheet" href="bootstrapValidator.min.css"/> <script type="text/javascript" src="bootstrapValidator.min.js"></script>
2.须要给表单加上一个 ID=‘’formva”,用来让bootstrap找到它,在页面载入的时候,就开始加载这段js,页面输入框输入的时候就开始判断,bootstarp还有不少规则,能够看下参考文章:http://www.cnblogs.com/huangcong/p/5335376.htmlide
// bootstrap validator $(document).ready(function() { $('#formva').bootstrapValidator({ message: 'This value is not valid', feedbackIcons: { valid: 'glyphicon glyphicon-ok', invalid: 'glyphicon glyphicon-remove', validating: 'glyphicon glyphicon-refresh' }, fields: { campaignName2: { message: 'The campaignName2 is not valid', validators: { notEmpty: { message: 'The campaignName2 is required and can\'t be empty' } } }, budget: { message: 'The budget is not valid', validators: { notEmpty: { message: 'The budget is required and can\'t be empty' }, stringLength: { min: 1, max: 10, message: 'The budget must be more than 1 and less than 10 characters long' }, numeric:{ message: 'The budget is required and can\'t be number' }, regexp: { regexp: /^([1-9]{1}[\d]{0,7}|0{1})(\.[\d]{1,2})?$/, message: 'The budget can only consist of number, dot ' } } } } }); });
3.在form表单中,若是提交按钮为submit,那么在点击提交的时候就会提示没有按照规则输入的表单数据,不容许提交,若是不是submit了,会提示,可是一样能够提交。 当用js控制提交的时候,须要在提交以前,调用下bootstrap-validator。函数
// bootstrap 的 表单验证 $('#formva').data('bootstrapValidator').validate(); if(!$('#formva').data('bootstrapValidator').isValid()){ return false; }
以上就是学习bootstrap的一点点记录,防止本身忘记,以备不时之需。若是错误,请留言指正。post