twitter引导模式——如何将数据传递给回调函数

twitter bootstrap modal -- how to pass data to callback function

本文关键字:函数 回调 数据 模式 twitter      更新时间:2023-09-26

有没有办法将额外的数据传递给引导模式函数回调?

例如,假设我的链接导致模态打开,其中有一个额外的属性,其中有一些有用的数据,我如何引用该属性?

HTML

<span listid="80" href="#editTaskList" data-toggle="modal" class="btn btn-mini right"><i class="icon-edit"></i> Edit Task List</span>

JS-

$('#editTaskList').on('show', function () {
    // get the source of the click, and then get the data i need.
});

这对你有用吗?

<span listid="80" href="#editTaskList" data-toggle="datamodal" class="btn btn-mini right"><i class="icon-edit"></i> Edit Task List</span>
var $editTaskList = $('#editTaskList');
$('body').on('click.modal.data-api', '[data-toggle="datamodal"]', function (e) {
    var $this = $(this);
    $editTaskList.data('anyAttr',$this.data('anyAttr'));
    $editTaskList.modal('show');
    e.preventDefault();
})
$editTaskList.on('show', function () {
    var myData = $editTaskList.data('anyAttr');
});

我知道这是一个老问题,但这是我在谷歌上发现的第一个问题。所以,我想把一些重要的信息放在这里。。。

在调用模态之前,您需要将回调函数绑定到事件上,例如:

$('#modal').on('shown', function(){ 
     // Do something when the modal is loaded
});
$("#modal").modal();

这非常重要,对我帮助很大,所以,现在是…

像这样-

<span id="modal_opener" data-extrastuff="stuff" listid="80" href="#editTaskList" data-toggle="modal" class="btn btn-mini right"><i class="icon-edit"></i> Edit Task List</span>
$('#modal_opener').click(function() {
    var stuff_i_want = $(this).attr('data-extrastuff');
});