我有一张 table 。在表格的单元格中有一个这样的链接:
<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>
模态框必须执行一些依赖于“data-id”属性值的操作。准确地说,在 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 代码中读取此属性的值。
非常感谢您的关注。
最佳答案
使用the .relatedTarget
property在Modal中提到事件文档:
$(document).ready(function () {
$('#remoteModal').on('show.bs.modal', function (event) {
console.log($(event.relatedTarget).attr('data-id'));
});
});
关于javascript - 如何将参数从链接元素传递到模态窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24951453/