如何将参数从link元素传递给模态窗口?

时间:2014-07-25 08:36:27

标签: javascript jquery twitter-bootstrap modal-dialog parameter-passing

我有一张桌子。在表的单元格中有一个这样的链接:     <a data-toggle="modal" data-id="xyz" href="#remoteModal" data-target="#remoteModal">SOME_TEXT</a>

当我点击此链接时应该打开一个模态。这是一个例子:

<div class="modal fade" id="remoteModal" tabindex="-1" role="dialog" aria-labelledby="remoteModal" aria-hidden="true">
  Some HTML/PHP Code
</div>

模态必须执行一些依赖于&#34; data-id&#34;的值的操作。属性。确切地说,在javascript代码中我需要读取此值:

<script type="text/javascript">
$(document).ready(function ( ) {
    $('#remoteModal').on('show.bs.modal', function( event ) {
        console.log( /* How do I read the value of data-id? */ );
    });
});

我不知道如何在模态的javascript代码中读取此属性的值。

非常感谢您的关注。

3 个答案:

答案 0 :(得分:6)

使用the .relatedTarget property活动文档中提到的Modal

$(document).ready(function () {
    $('#remoteModal').on('show.bs.modal', function (event) {
        console.log($(event.relatedTarget).attr('data-id'));
    });
});

答案 1 :(得分:0)

//Global Variable
value = '';

$('[href = #remoteModal]').click(function(event){
        event.preventDefault();
        value = $(this).data("id");
        $('#remoteModal').show();
});

我认为您可以在此页面的任何位置访问value变量

答案 2 :(得分:0)

var data-id = $(this).attr('data-id');

所以在 data-id 中,您将获得&#34; X#34;