前端框架-Bootstrap【搭建后台管理系统】
模态框:
不同的按钮打开同一个id=myModal的模态框,有时候需要传入一个参数(flag)来进行区分是哪一个按钮打开的
那么直接上代码了
第一个按钮button属性里面加入data-whatever="add"
1 <button id="sure_pre_release_draft" type="button" data-toggle="modal" data-target="#myModal" data-whatever="add" class="btn btn-primary btn-sm"> 2 <span class="glyphicon glyphicon-plus"></span> 3 </button>
第二个按钮button属性里面加入data-whatever="edit"
1 <button id="sure_pre_release_draft" type="button" data-toggle="modal" data-target="#myModal" data-whatever="edit" class="btn btn-primary btn-sm"> 2 <span class="glyphicon glyphicon-plus"></span> 3 </button>
然后通过模态框触发事件来获取
1 $("#myModal").on("show.bs.modal",function(e){ 2 //获得点击打开的按钮 3 var button=$(e.relatedTarget); 4 //根据标签获得按钮传入的参数 5 var recipient=button.data("whatever"); 6 //console.log(recipient); 7 if(recipient == "edit"){ 8 do something............. 9 } 10 if(recipient == "add"){ 11 do something............. 12 } 13 });
原理来源:https://v3.bootcss.com/javascript/#modals-related-target
人生苦短,我用python!