在hide.bs.modal上获取bootstrap模态元素id

时间:2016-05-05 16:54:06

标签: javascript jquery twitter-bootstrap

我希望在bootstrap中返回当前隐藏模式的元素ID。我似乎无法通过此代码找到捕获它的方法:

$(document).on('hide.bs.modal', function (e) {

     // I want to know what the attr ID is of THIS modal being closed?
     // I tried console.log(e) to see if I could find the reference but its not in the object?

});

目的是为所有模态设置此项,某些模态包含输入字段,如果更改为脏标志,我可以提醒用户如果关闭此模式可能会发生数据丢失。

2 个答案:

答案 0 :(得分:1)

如果您在该处理程序函数中记录您收到的事件,您会注意到它们上面有一个包含实际模态的target节点。所以获取id应该如下:

var id = e.target.id;

查看我设置的演示:
http://www.bootply.com/JvORc5bWvD
关闭时,您将在控制台中看到模态ID。

答案 1 :(得分:0)

enter image description here

我可以看到,当我在回归时,我得到一个空目标。但是,如果我发出警报(e.target.id),它会返回正确的ID。谢谢!