twitter-bootstrap Bootstrap 模式相关目标未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26187370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bootstrap modal relatedTarget is undefined
提问by user3312508
I am trying to get the clicked element using the property relatedTargetof the show.bs.modalevent. It's always getting undefined though.
我试图让使用属性的点击的元素relatedTarget中的 show.bs.modal事件。但它总是变得不确定。
This is what I have:
这就是我所拥有的:
$("#curCarSelect").on('click', function(e) {
$("#curCarModal").on('show.bs.modal', function(event) {
modalOpenedby = event.relatedTarget;
alert(modalOpenedby);
}).modal('toggle');
});
回答by Antonio
try:
尝试:
$("#curCarSelect").on('click', function(e) {
$modal.modal('toggle', $(this));
});
where $modal is your modal element to open, then:
其中 $modal 是您要打开的模态元素,然后:
$modal.on('show.bs.modal', function (event) {
var button = $(event.relatedTarget) // Button that triggered the modal
})
回答by cvrebert
根据文档:
If caused by a click, the clicked element is available as the
relatedTargetproperty of the event.
如果由 click 引起,则被点击的元素可用作
relatedTarget事件的属性。
But you're just calling .modal('toggle'). That doesn't involve any click event, hence why relatedTargetin undefined in your case.
但你只是在打电话.modal('toggle')。这不涉及任何 click event,因此为什么relatedTarget在您的情况下为 undefined 。
回答by maksbd19
If you need to use event.relatedTargetin you dynamically opened modal then you can pass the target as a second argument in the modal function.
如果您需要event.relatedTarget在动态打开的模态中使用,那么您可以将目标作为模态函数中的第二个参数传递。
$("#curCarSelect").on('click', function(e) {
$("#curCarModal").modal('toggle', $("#curCarSelect"));
});
回答by SudarP
Workaround for this worked for me on bootstrap 2:
对此的解决方法在引导程序 2 上对我有用:
$("a[data-toggle='modal']").on('click', function(e) {
$_clObj = $(this); //clicked object
$_mdlObj = $_clObj.attr('data-target'); //modal element id
$($_mdlObj).on('shown.bs.modal',{ _clObj: $_clObj }, function (event) {
$_clObj = event.data._clObj; //$_clObj is the clicked element !!!
//do your stuff here...
});
});
回答by stasionok
I make it worked for me. Try this:
我让它对我来说有效。试试这个:
$("#curCarSelect").on('click', function(e) {
$("#curCarModal").on('show.bs.modal', function(event) {
modalOpenedby = event.relatedTarget;
alert(modalOpenedby);
}).modal('toggle', e);
});
回答by Fergoso
try this:
试试这个:
$('#curCarModal').on('shown.bs.modal', function () {
modalOpenedby = event.relatedTarget;
});
$("#curCarSelect").on('click', function(e) {
$("#curCarModal").modal('toggle');
alert(modalOpenedby);
});

