将数据传递给一个引导模式 [英] Passing data to a bootstrap modal
问题描述
我有一对夫妇的超链接,每个连接有一个ID的。当我点击这个链接,我想开一个模态(<一个href=\"http://twitter.github.com/bootstrap/javascript.html#modals\">http://twitter.github.com/bootstrap/javascript.html#modals ),并将该ID传递给模态。我搜索谷歌,但我找不到任何可以帮助我。
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.
这是code:
<a data-toggle="modal" data-id="@book.Id" title="Add this item" class="open-AddBookDialog"></a>
会打开:
<div class="modal hide" id="addBookDialog">
<div class="modal-body">
<input type="hidden" name="bookId" id="bookId" value=""/>
</div>
</div>
通过这块code的:
$(document).ready(function () {
$(".open-AddBookDialog").click(function () {
$('#bookId').val($(this).data('id'));
$('#addBookDialog').modal('show');
});
});
然而,当我点击超链接,没有任何反应。当我给的超级链接A HREF =#addBookDialog,模态开得很好,但它简化版,包含任何数据。
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.
我跟着这个例子:<一href=\"http://stackoverflow.com/questions/10379624/how-to-pass-values-arguments-to-modal-show-function-in-twitter-bootstrat\">How将值传递参数给modal.show()函数在微博bootstrat
(我也试过这样:如何在模式设置对话输入值?)
推荐答案
我觉得你可以使用,使这项工作jQuery的。对事件处理程序。
I think you can make this work using jQuery's .on event handler.
下面是你可以测试一个小提琴;只要确保展开的HTML框架
小提琴尽可能让您可以查看模式。
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> </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 superfluous to have to manually call the modal.
// $('#addBookDialog').modal('show');
});
这篇关于将数据传递给一个引导模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!