How to pass a parameter from a link element to a modal window?(如何将参数从链接元素传递到模式窗口?)
问题描述
我有一张桌子.在表格的一个单元格中有一个这样的链接:<a data-toggle="modal" data-id="xyz" href="#remoteModal" data-target="#remoteModal">SOME_TEXT</a>
.
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>
模态必须执行一些依赖于data-id"属性值的操作.准确地说,在 javascript 代码中我需要读取这个值:
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? */ );
});
});
我不知道如何在modal的javascript代码中读取这个属性的值.
I do not know how to read the value of this attribute in the javascript code of the modal.
非常感谢您的关注.
推荐答案
使用Modal 事件文档中提到的 .relatedTarget
属性:
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'));
});
});
这篇关于如何将参数从链接元素传递到模式窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将参数从链接元素传递到模式窗口?


基础教程推荐
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
- 每次设置弹出窗口的焦点 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01