我有几个超链接,每个超链接都附有一个ID。当我点击此链接时,我想打开一个模态(http://twitter.github.com/bootstrap/javascript.html#modals),并将此ID传递给模态。我在谷歌搜索,但找不到任何可以帮助我的东西。
这是代码:
<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>
使用这段代码:
$(document).ready(function () {
$(".open-AddBookDialog").click(function () {
$('#bookId').val($(this).data('id'));
$('#addBookDialog').modal('show');
});
});
然而,当我点击超链接时,没有任何反应。当我给出超链接<a href="#addBookDialog" ...>
时,模态打开就好了,但它不包含任何数据。
我按照这个例子:How to pass values arguments to modal.show() function in Bootstrap
答案 0 :(得分:478)
我认为你可以使用jQuery的.on事件处理程序来完成这项工作。
这是你可以测试的小提琴;只需确保尽可能扩展小提琴中的HTML框架,以便查看模态。
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 unnecessary to have to manually call the modal.
// $('#addBookDialog').modal('show');
});
答案 1 :(得分:318)
如果您使用 Bootstrap 3.2.0 ,这是一种更简洁的方法。
链接HTML
<a href="#my_modal" data-toggle="modal" data-book-id="my_id_value">Open Modal</a>
模态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);
});
答案 2 :(得分:31)
以下是我使用@ mg1075代码实现它的方法。我想要一些更通用的代码,以便不必将类分配给模态触发链接/按钮:
在Twitter Bootstrap 3.0.3中测试。
<强> HTML 强>
<a href="#" data-target="#my_modal" data-toggle="modal" data-id="my_id_value">Open Modal</a>
<强> 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);
})
});
答案 3 :(得分:10)
如果您使用的是3+引导程序,如果使用Jquery,则可以进一步缩短。
<强> 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 强>
<script type="text/javascript">
$(function () {
$(".identifyingClass").click(function () {
var my_id_value = $(this).data('id');
$(".modal-body #hiddenValue").val(my_id_value);
})
});
</script>
答案 4 :(得分:6)
您的代码可以使用正确的模态html结构。
$(function(){
$(".open-AddBookDialog").click(function(){
$('#bookId').val($(this).data('id'));
$("#addBookDialog").modal("show");
});
});
&#13;
<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>
&#13;
答案 5 :(得分:2)
使用jquery很简单:
如果下面是您的定位链接:
<a data-toggle="modal" data-id="@book.Id" title="Add this item" class="open-AddBookDialog"></a>
在模态的show事件中,您可以访问如下所示的定位标记
//triggered when modal is shown
$('#modal_id').on('shown.bs.modal', function(event) {
// The reference tag is your anchor tag here
var reference_tag = $(event.relatedTarget);
var id = reference_tag.data('id')
// ...
// ...
})
答案 6 :(得分:2)
Bootstrap 4.0提供了一个修改选项 使用jquery的模式数据:https://getbootstrap.com/docs/4.0/components/modal/#varying-modal-content
这是文档上的脚本标签:
$('#exampleModal').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget) // Button that triggered the modal
var recipient = button.data('whatever') // Extract info from data-* attributes
// If necessary, you could initiate an AJAX request here (and then do the updating in a callback).
// Update the modal's content. We'll use jQuery here, but you could use a data binding library or other methods instead.
var modal = $(this)
modal.find('.modal-title').text('New message to ' + recipient)
modal.find('.modal-body input').val(recipient)
})
它在大多数情况下都起作用。仅对模式的调用不起作用。 这是对我有用的脚本的修改:
$(document).on('show.bs.modal', '#exampleModal',function(event){
... // same as in above script
})
答案 7 :(得分:1)
这是将id_data发送到模态的方法:
<input
href="#"
data-some-id="uid0123456789"
data-toggle="modal"
data-target="#my_modal"
value="SHOW MODAL"
type="submit"
class="btn modal-btn"/>
<div class="col-md-5">
<div class="modal fade" id="my_modal">
<div class="modal-body modal-content">
<h2 name="hiddenValue" id="hiddenValue" />
</div>
<div class="modal-footer" />
</div>
和javascript:
$(function () {
$(".modal-btn").click(function (){
var data_var = $(this).data('some-id');
$(".modal-body h2").text(data_var);
})
});
答案 8 :(得分:0)
您可以尝试simpleBootstrapDialog。在这里您可以传递标题,消息,用于取消和提交等的回调选项...
答案 9 :(得分:0)
在Bootstrap 4.3中,使用以下代码段中的代码-更多here
$('#exampleModal').on('show.bs.modal', function (event) {
let bookId = $(event.relatedTarget).data('bookid')
$(this).find('.modal-body input').val(bookId)
})
a.btn.btn-primary.open-AddBookDialog {color: white }
<!-- Initialize Bootstrap 4 -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<!-- BUTTON -->
<a
class="btn btn-primary open-AddBookDialog"
data-toggle="modal"
data-target="#exampleModal"
data-bookid="@book.Id"
title="Add this item"
>Open</a>
<!-- MODAL -->
<div class="modal fade" id="exampleModal" 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=""/>
<input type="text" class="form-control" id="recipient-name">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
答案 10 :(得分:0)
我发现这种方法很有用:
创建点击事件:
$( button ).on('click', function(e) {
let id = e.node.data.id;
$('#myModal').modal('show', {id: id});
});
创建show.bs.modal事件:
$('#myModal').on('show.bs.modal', function (e) {
// access parsed information through relatedTarget
console.log(e.relatedTarget.id);
});
其他:
在show.bs.modal内部进行逻辑检查以检查属性是否已解析,例如这样:
id:( e.relatedTarget.hasOwnProperty( 'id' ) ? e.relatedTarget.id : null )
答案 11 :(得分:0)
发现这对我有用:
在链接中:
<button type="button" class="btn btn-success" data-toggle="modal" data-target="#message<?php echo $row['id'];?>">Message</button>
在模式中:
<div id="message<?php echo $row['id'];?>" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<p>Some text in the modal.</p>
<?php echo $row['id'];?>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
答案 12 :(得分:0)
例如,对于更新 href 值,我会执行以下操作:
<a href="#myModal" data-toggle="modal" data-url="http://example.com">Show modal</a>
$('#myModal').on('show.bs.modal', function (e) {
var url = $(e.relatedTarget).data('url');
$(e.currentTarget).find('.any-class').attr("href", "url");
})