将数据传递给引导模态

我有两个超链接,每个都有一个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" ...>时,模态打开得很好,但它不包含任何数据。

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

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

1058958 次浏览

我认为您可以使用jQuery的内用事件处理程序来实现这一点。

这是一个你可以测试的小提琴;只要确保在小提琴中尽可能地展开HTML框架,这样你就可以查看模式。

http://jsfiddle.net/8c05pn1f/

超文本标记语言

<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');
});

下面是我如何从@mg1075的代码实现它。我想要更多的泛型代码,这样就不必为模态触发器链接/按钮分配类:

在Twitter Bootstrap 3.0.3中测试。

超文本标记语言

<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.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);
});

http://jsfiddle.net/k7FC2/

试试这个

$(function(){
//when click a button
$("#miButton").click(function(){
$(".dynamic-field").remove();
//pass the data in the modal body adding html elements
$('#myModal .modal-body').html('<input type="hidden" name="name" value="your value" class="dynamic-field">') ;
//open the modal
$('#myModal').modal('show')
})
})

如果你是在引导3+,这可以缩短一点,如果使用Jquery。

超文本标记语言

<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">&times;</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>

您的代码将与正确的modal 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>

您可以尝试simpleBootstrapDialog。在这里你可以传递标题,消息,回调选项取消和提交等…

使用此插件包括simpleBootstrapDialog.js文件如下所示

<script type="text/javascript" src="/simpleDialog.js"></script>

基本用法

<script type="text/javascript>
$.simpleDialog();
</script>

自定义标题和描述

$.simpleDialog({
title:"Alert Dialog",
message:"Alert Message"
});


与回调

<script type="text/javascript>
$.simpleDialog({
onSuccess:function(){
alert("You confirmed");
},
onCancel:function(){
alert("You cancelled");
}
});
</script>

引导4.3 -使用下面的代码片段-更多的在这里

$('#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>

这是你如何发送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);
})
});

这是如此简单的jquery:

如果下面是你的锚链接:

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


在你的模态的显示事件中,你可以像下面这样访问锚标记

//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')
// ...
// ...
})




我发现这个方法很有用:

创建点击事件:

$( 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: # EYZ0 < / p >
Bootstrap 4.0给出了一个修改选项 jquery: https://getbootstrap.com/docs/4.0/components/modal/#varying-modal-content

下面是文档中的script标签:

$('#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)
})

它在大多数情况下都是有效的。只有对modal的调用不工作。 下面是对脚本的修改,为我工作:

$(document).on('show.bs.modal', '#exampleModal',function(event){
... // same as in above script
})

我发现这对我有用:

在链接中:

<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">&times;</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>

例如,对于更新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");
})

我是bootstrap 5,由于我控制之外的约束,我不能显示()脚本的模态,所以我依赖于弹出模态的按钮的属性data-b -toggle和data-b -target。

Bootstrap暴露了一些事件(https://getbootstrap.com/docs/5.0/components/modal/),我们可以像这样钩住代码:

document.getElementById('idModal').addEventListener('show.bs.modal', (e) => {
console.log(e.relatedTarget);
});

在relatedTarget中,我们将在dataset中找到指定的数据属性,例如,如果我们有一个看起来像这样的元素:

<button
class="btn btn-secondary"
data-id="fk_articles"
data-bs-toggle="modal"
data-bs-target="#documentationModal">
Documentation
</button>

然后我们可以通过以下方式到达fk_articles:

console.log( e.relatedTarget.dataset.id )

我认为你不需要jQuery。您可以使用onclick事件并将bookId传递给javascript函数,在那里您可以查询元素并设置输入值。

# EYZ0

而且

<script>
function passBookId(bookId) {
let inputElement= document.document.querySelector("div.modal-dialog input[name='bookId'");
if (inputElement) {
inputElement.value = bookId;
}
return true;
}
</script>