How to pass a parameter from a link element to a modal window?

安稳与你 提交于 2019-11-28 09:53:02

问题


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.


回答1:


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'));
    });
});



回答2:


//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




回答3:


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

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



来源:https://stackoverflow.com/questions/24951453/how-to-pass-a-parameter-from-a-link-element-to-a-modal-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!