How to pass a parameter from a link element to a m

2019-01-28 14:28发布

I have a table. In a cell of the table there is a link like this: <a data-toggle="modal" data-id="xyz" href="#remoteModal" data-target="#remoteModal">SOME_TEXT</a>.

When I click on this link should open a modal. Here's an example:

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

The modal must perform some operations that depend on the value of "data-id" attribute. To be precise, in the javascript code I need to read this value:

<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? */ );
    });
});

I do not know how to read the value of this attribute in the javascript code of the modal.

Many thanks for your interest.

3条回答
虎瘦雄心在
2楼-- · 2019-01-28 14:38
var data-id = $(this).attr('data-id');

So here in data-id you wll get your "XYZ"

查看更多
一纸荒年 Trace。
3楼-- · 2019-01-28 14:49
//Global Variable
value = '';

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

And I think you can access to value variable everywhere in this page

查看更多
祖国的老花朵
4楼-- · 2019-01-28 14:52

Use the .relatedTarget property mentioned in the Modal Event docs:

$(document).ready(function () {
    $('#remoteModal').on('show.bs.modal', function (event) {
        console.log($(event.relatedTarget).attr('data-id'));
    });
});
查看更多
登录 后发表回答