简体   繁体   English

如何将参数从链接元素传递到模式窗口?

[英]How to pass a parameter from a link element to a modal window?

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> .在表格的一个单元格中有一个这样的链接: <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.模态必须执行一些依赖于“data-id”属性值的操作。 To be precise, in the javascript code I need to read this value:准确地说,在 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? */ );
    });
});

I do not know how to read the value of this attribute in the javascript code of the modal.我不知道如何在模态的javascript代码中读取这个属性的值。

Many thanks for your interest.非常感谢您的关注。

Use the .relatedTarget property mentioned in the Modal Event docs:使用模态事件文档中提到.relatedTarget属性

$(document).ready(function () {
    $('#remoteModal').on('show.bs.modal', function (event) {
        console.log($(event.relatedTarget).attr('data-id'));
    });
});
//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我认为你可以在这个页面的任何地方访问value变量

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

所以在data-id 中你会得到你的“XYZ”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM