将数据传递给Bootstrap 3 Modal [英] Passing data to Bootstrap 3 Modal

查看:229
本文介绍了将数据传递给Bootstrap 3 Modal的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含大量数据的表,并且我在每行的末尾放置了一个按钮,我想用它来触发模态并将该行的唯一ID传递给模态。

I have a table with a bunch of data, and I've placed a button at the end of each row that I want to use to trigger a modal and pass the unique id of that row into the modal.

我找到了这个答案:将数据传递给引导程序模式

但它似乎不适用于Bootstrap 3,我无法找到任何有关此任何想法的数据?

But it doesn't appear to work with Bootstrap 3, and I can't find any data about this anywhere any ideas?

我使用的代码相同:

标题:

<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href = "bootstrap/css/bootstrap.min.css" rel = "stylesheet">
<link href = "bootstrap/css/styles.css" rel = "stylesheet">
<script type="text/javascript">
$(document).on("click", ".open-AddBookDialog", function () {
var myBookId = $(this).data('id');
$(".modal-body #bookId").val(myBookId);
});
</script>
</head>

正文:

<a data-toggle="modal" data-id="ISBN" title="Add this item" class="open-AddBookDialog btn btn-primary" href="#addBookDialog">test</a>

模态:

<div class="modal" id="addBookDialog">
<div class="modal-header">
<button class="close" data-dismiss="modal">×</button>
<h3>Modal header</h3>

</div>
<div class="modal-body">
<p>some content</p>
<input type="text" name="bookId" id="bookId" value="" />
</div>
</div>

结果页面将打开模态,但文本字段为空。

The resulting page will open the modal, but the text field is blank.

我忘了提及,我确实加载了jquery和bootstrap:

I forgot to mention, I do have both jquery and bootstrap loaded:

<script src = "http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src = "bootstrap/js/bootstrap.js"></script>


推荐答案

尝试手动触发模态,而不是使用数据API。

Try manually triggering the modal, rather than using the data API.

像这样更新超链接:

<a data-id="ISBN" title="Add this item" class="open-AddBookDialog btn btn-primary" href="#addBookDialog">test</a>

然后调整你的jQuery代码:

Then adjust your jQuery code:

$(document).on("click", ".open-AddBookDialog", function (e) {

    e.preventDefault();

    var _self = $(this);

    var myBookId = _self.data('id');
    $("#bookId").val(myBookId);

    $(_self.attr('href')).modal('show');
});

似乎对我有用; jsFiddle @ http://jsfiddle.net/4XJ54/

Seems to work for me; jsFiddle @ http://jsfiddle.net/4XJ54/

这篇关于将数据传递给Bootstrap 3 Modal的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆