Bootstrap模态框调用功能实现方法

近在做一个项目时需要在页面中嵌入弹出窗口,用来展示表单信息。其实这种弹出窗口有很多jquery插件都可以实现,我这边前端都是用Bootstrap的,正好Bootstrap有模态框这个功能,这下就可以直接拿它实现了。

实现步骤如下:

在前端页面引入bootstrap相关的css和js文件

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css"> <script type='text/javascript' src='https://www.jb51.net/public/js/jquery-1.8.2.min.js'></script> <script src="https://libs.baidu.com/jquery/1.9.0/jquery.js"></script> <script src="https://cdn.bootcss.com/bootstrap/2.3.1/js/bootstrap-transition.js"></script> <script src="https://cdn.bootcss.com/bootstrap/2.3.1/js/bootstrap-modal.js"></script>

然后在html中创建一个button用来点击触发弹出窗口,这里是调用f_show函数并传入一个参数向后端发起Ajax请求,Ajax处理请求后返回前端并输出到模态框。

<td><button type="button" data-toggle="modal" data-target="#myModal" ><?php echo $row['service_type'];?></button></td>

Ajax处理请求

<script> function f_show(tags){ $.ajax({ type:"POST", url:"service_info_ajax.php", data: {"tags":tags}, dataType:'json', success: function(data){ if(data.state == '1'){ $("#service_key").val(data.service_key); $("#service_comments").val(data.service_comments); $("#service_help").val(data.service_help); $("#service_target").val(data.service_target); $("#service_mail").val(data.service_mail); $("#service_mailpass").val(data.service_mailpass); $("#service_date").val(data.service_date); if(data.service_type <= '2'){ document.getElementById("remark1").style.display = "none"; document.getElementById("remark2").style.display = "none"; document.getElementById("notes1").style.display = ""; document.getElementById("notes2").style.display = ""; $("#notes2").text(data.service_notes); }else{ document.getElementById("notes1").style.display = "none"; document.getElementById("notes2").style.display = "none"; document.getElementById("remark1").style.display = ""; document.getElementById("remark2").style.display = ""; $("#remark2").text(data.service_remark); } } }, }); } $('#myModal').modal(options); </script>

后端处理返回

<?php require_once("../db.class.php"); require_once("../common.class.php"); require_once("../mailtmp.class.php"); require_once("../admin.class.php"); $obj_comm = new Comm(); $obj_mail = new mailtemplate(); $obj_admin = new AdminSub(); $tags = trim($_POST['tags']); #$tags = "1643e0cb3d6078a9a0fd86c8239cd4c1"; $tag_arr = $obj_admin->find_service_tag($tags,$db); if($tag_arr){ $tag_arr['state'] = 1; echo json_encode($tag_arr); } ?>

前端模态框页面展示

<!--模态框开始--><div role="dialog" aria-labelledby="gridSystemModalLabel"> <div role="document"> <div> <div> <button type="button" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button> <h4>Service</h4> </div> <div> <div> <div> <div> <label for="form-username">研究关键字</label> <input value="<?php echo $ids;?>" readonly="readonly" type="text" placeholder=""> </div> </div> <div> <div> <label for="form-username">评审意见</label> <input readonly="readonly" type="text" placeholder=""> </div> </div> <div> <div> <label for="form-username">帮助选刊</label> <input readonly="readonly" type="text" placeholder=""> </div> </div> <div> <div> <label for="form-username">投稿服务</label> <input readonly="readonly" type="text" placeholder=""> </div> </div> <div> <div> <label for="form-username">投稿邮箱</label> <input readonly="readonly" type="text" placeholder=""> </div> </div> <div> <div> <label for="form-username">邮箱密码</label> <input readonly="readonly" type="text" placeholder=""> </div> </div> <div> <div> <label for="form-username">投稿加急</label> <input readonly="readonly" type="text" placeholder=""> </div> </div> <div> <div> <label for="form-username">改写需求</label> <textarea cols="20" rows="3" readonly="readonly" placeholder=""></textarea> <label for="form-username">投稿备注</label> <textarea cols="20" rows="3" readonly="readonly" placeholder=""></textarea> </div> </div> </div> </div> <div> <button type="button" data-dismiss="modal">Close</button> </div> </div><!-- /.modal-content --> </div><!-- /.modal-dialog --> </div><!-- /.modal --> <!--模态框结束-->

实现效果如图:

Bootstrap模态框调用功能实现方法

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wzjjfd.html