将数据传递给引导模式 [英] Passing data to a bootstrap modal

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

问题描述

我有几个超链接,每个超链接都附有一个 ID.当我点击这个链接时,我想打开一个模态( http://twitter.github.com/bootstrap/javascript.html#modals".com/bootstrap/javascript.html#modals ),并将此 ID 传递给模态.我在谷歌上搜索,但找不到任何可以帮助我的东西.

这是代码:

<a data-toggle="modal" data-id="@book.Id" title="添加此项" class="open-AddBookDialog"></a>

哪个应该打开:

用这段代码:

$(document).ready(function () {$(".open-AddBookDialog").click(function () {$('#bookId').val($(this).data('id'));$('#addBookDialog').modal('show');});});

但是,当我单击超链接时,没有任何反应.当我提供超链接 <a href="#addBookDialog" ...> 时,模式打开得很好,但它不包含任何数据.

我遵循了这个例子:如何在 Bootstrap 中将值参数传递给 modal.show() 函数

(我也试过这个:如何在模态对话框中设置输入值?)

解决方案

我认为你可以使用 jQuery 的 .on 事件处理程序.

这是一个你可以测试的小提琴;只需确保尽可能多地扩展小提琴中的 HTML 框架,以便您可以查看模态.

http://jsfiddle.net/Au9tc/605/

HTML

链接1

<a data-toggle="modal" data-id="ISBN564541" title="添加此项" class="open-AddBookDialog btn btn-primary" href="#addBookDialog">test</a><p>&nbsp;</p><p>链接2</p><a data-toggle="modal" data-id="ISBN-001122" title="添加此项" class="open-AddBookDialog btn btn-primary" href="#addBookDialog">test</a><div class="modal hide" id="addBookDialog"><div class="modal-header"><button class="close" data-dismiss="modal">×</button><h3>模态头</h3>

<div class="modal-body"><p>一些内容</p><input type="text" name="bookId" id="bookId" value=""/>

JAVASCRIPT

$(document).on("click", ".open-AddBookDialog", function () {var myBookId = $(this).data('id');$(".modal-body #bookId").val(myBookId);//正如评论中指出的那样,//不必手动调用模态.//$('#addBookDialog').modal('show');});

I've got a couple of hyperlinks that each have an ID attached. When I click on this link, I want to open a modal ( http://twitter.github.com/bootstrap/javascript.html#modals ), and pass this ID to the modal. I searched on google, but I couldn't find anything that could help me.

This is the code:

<a data-toggle="modal" data-id="@book.Id" title="Add this item" class="open-AddBookDialog"></a>

Which should open:

<div class="modal hide" id="addBookDialog">
    <div class="modal-body">
        <input type="hidden" name="bookId" id="bookId" value=""/>
    </div>
</div>

With this piece of code:

$(document).ready(function () {
    $(".open-AddBookDialog").click(function () {
        $('#bookId').val($(this).data('id'));
        $('#addBookDialog').modal('show');
    });
});

However, when I click the hyperlink, nothing happens. When I give the hyperlink <a href="#addBookDialog" ...>, the modal opens just fine, but it does't contain any data.

I followed this example: How to pass values arguments to modal.show() function in Bootstrap

(and I also tried this: How to set the input value in a modal dialogue?)

解决方案

I think you can make this work using jQuery's .on event handler.

Here's a fiddle you can test; just make sure to expand the HTML frame in the fiddle as much as possible so you can view the modal.

http://jsfiddle.net/Au9tc/605/

HTML

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

<p>&nbsp;</p>


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

<div class="modal hide" 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>

JAVASCRIPT

$(document).on("click", ".open-AddBookDialog", function () {
     var myBookId = $(this).data('id');
     $(".modal-body #bookId").val( myBookId );
     // As pointed out in comments, 
     // it is unnecessary to have to manually call the modal.
     // $('#addBookDialog').modal('show');
});

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

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆