本文翻譯自:Passing data to a bootstrap modal
I've got a couple of hyperlinks that each have an ID attached. 我有幾個超鏈接,每個超鏈接都附有一個ID。 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. 當我點擊此鏈接時,我想打開一個模態( http://twitter.github.com/bootstrap/javascript.html#modals ),並將此ID傳遞給模態。 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. 當我給超鏈接<a href="#addBookDialog" ...>
,模態打開就好了,但它不包含任何數據。
I followed this example: How to pass values arguments to modal.show() function in Bootstrap 我按照這個例子: 如何將值參數傳遞給Bootstrap中的modal.show()函數
(and I also tried this: How to set the input value in a modal dialogue? ) (我也試過這個: 如何在模態對話框中設置輸入值? )
#1樓
參考:https://stackoom.com/question/iaXN/將數據傳遞給bootstrap模式
#2樓
I think you can make this work using jQuery's .on event handler. 我認爲你可以使用jQuery的.on事件處理程序來完成這項工作。
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. 只需確保儘可能擴展小提琴中的HTML框架,以便查看模態。
http://jsfiddle.net/Au9tc/605/ http://jsfiddle.net/Au9tc/605/
HTML 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 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');
});
#3樓
Here's how I implemented it working from @mg1075's code. 以下是我使用@ mg1075代碼實現它的方法。 I wanted a bit more generic code so as not to have to assign classes to the modal trigger links/buttons: 我想要一些更通用的代碼,以便不必爲模態觸發鏈接/按鈕分配類:
Tested in Twitter Bootstrap 3.0.3. 在Twitter Bootstrap 3.0.3中測試。
HTML HTML
<a href="#" data-target="#my_modal" data-toggle="modal" data-id="my_id_value">Open Modal</a>
JAVASCRIPT JAVASCRIPT
$(document).ready(function() {
$('a[data-toggle=modal], button[data-toggle=modal]').click(function () {
var data_id = '';
if (typeof $(this).data('id') !== 'undefined') {
data_id = $(this).data('id');
}
$('#my_element_id').val(data_id);
})
});
#4樓
Here is a cleaner way to do it if you are using Bootstrap 3.2.0 . 如果您使用的是Bootstrap 3.2.0,這是一種更簡潔的方法 。
Link HTML 鏈接HTML
<a href="#my_modal" data-toggle="modal" data-book-id="my_id_value">Open Modal</a>
Modal JavaScript 模態JavaScript
//triggered when modal is about to be shown
$('#my_modal').on('show.bs.modal', function(e) {
//get data-id attribute of the clicked element
var bookId = $(e.relatedTarget).data('book-id');
//populate the textbox
$(e.currentTarget).find('input[name="bookId"]').val(bookId);
});
http://jsfiddle.net/k7FC2/ http://jsfiddle.net/k7FC2/
#5樓
If you are on bootstrap 3+, this can be shortened a bit further if using Jquery. 如果你在bootstrap 3+上,如果使用Jquery,這可以進一步縮短。
HTML HTML
<a href="#" data-target="#my_modal" data-toggle="modal" class="identifyingClass" data-id="my_id_value">Open Modal</a>
<div class="modal fade" id="my_modal" tabindex="-1" role="dialog" aria-labelledby="my_modalLabel">
<div class="modal-dialog" role="dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Modal Title</h4>
</div>
<div class="modal-body">
Modal Body
<input type="hidden" name="hiddenValue" id="hiddenValue" value="" />
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">No</button>
<button type="button" class="btn btn-primary">Yes</button>
</div>
</div>
</div>
JQUERY JQUERY
<script type="text/javascript">
$(function () {
$(".identifyingClass").click(function () {
var my_id_value = $(this).data('id');
$(".modal-body #hiddenValue").val(my_id_value);
})
});
</script>
#6樓
Your code would have worked with correct modal html structure. 您的代碼可以使用正確的模態html結構。
$(function(){ $(".open-AddBookDialog").click(function(){ $('#bookId').val($(this).data('id')); $("#addBookDialog").modal("show"); }); });
<html> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> <a data-id="@book.Id" title="Add this item" class="open-AddBookDialog">Open Modal</a> <div id="addBookDialog" class="modal fade" tabindex="-1" role="dialog"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-body"> <input type="hidden" name="bookId" id="bookId" value=""/> </div> </div><!-- /.modal-content --> </div><!-- /.modal-dialog --> </div><!-- /.modal --> </html>