不同的按钮打开同一个id=myModal的模态框,有时候需要传入一个参数(flag)来进行区分是哪一个按钮打开的
那么直接上代码了
第一个按钮button属性里面加入data-whatever="add"
<button id="sure_pre_release_draft" type="button" data-toggle="modal" data-target="#myModal" data-whatever="add" class="btn btn-primary btn-sm">
<span class="glyphicon glyphicon-plus"></span>
</button>
第一个按钮button属性里面加入data-whatever="edit"
<button id="sure_pre_release_draft" type="button" data-toggle="modal" data-target="#myModal" data-whatever="edit" class="btn btn-primary btn-sm">
<span class="glyphicon glyphicon-plus"></span>
</button>
然后通过模态框触发事件来获取
$("#myModal").on("show.bs.modal",function(e){
//获得点击打开的按钮
var button=$(e.relatedTarget);
//根据标签获得按钮传入的参数
var recipient=button.data("whatever");
//console.log(recipient);
if(recipient == "edit"){
do something.............
}
if(recipient == "add"){
do something.............
}
});