gpt4 book ai didi

jquery - 如何在 Bootstrap 3 中将变量传递给模态

转载 作者:行者123 更新时间:2023-12-01 01:30:50 25 4
gpt4 key购买 nike

我已经为此苦苦挣扎了几天。我尝试了很多不同的解决方案他们都不适合我。然后我发现了以下链接,它似乎是正是我正在寻找的。 https://codepen.io/Oza94/pen/ZbERWy我完全复制了来源,到我的本地主机以及我的 godaddy 域。模型开放,但数据“trodbal trodbal”不像在 codepen 示例中那样出现在我的服务器上。有人可以帮忙吗?

<!DOCTYPE html>
<html lang="en">

<head>

<title>Pass Modal Vars</title>

<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=Edge">
<meta name="description" content="">
<meta name="keywords" content="">
<meta name="author" content="">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">

<script>
// data-* attributes to scan when populating modal values
var ATTRIBUTES = ['myvalue', 'myvar', 'bb'];

$('[data-toggle="modal"]').on('click', function(e) {
// convert target (e.g. the button) to jquery object
var $target = $(e.target);
// modal targeted by the button
var modalSelector = $target.data('target');

// iterate over each possible data-* attribute
ATTRIBUTES.forEach(function(attributeName) {
// retrieve the dom element corresponding to current attribute
var $modalAttribute = $(modalSelector + ' #modal-' + attributeName);
var dataValue = $target.data(attributeName);

// if the attribute value is empty, $target.data() will return undefined.
// In JS boolean expressions return operands and are not coerced into
// booleans. That way is dataValue is undefined, the left part of the following
// Boolean expression evaluate to false and the empty string will be returned
$modalAttribute.text(dataValue || '');
});
});
</script>
</head>

<body>

<!-- Button trigger modal -->
<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal" data-myvalue="trouducul" data-myvar="bisbis">
Launch demo modal 1
</button>

<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal" data-myvalue="troudbal" data-bb="troudbal">
Launch demo modal 2
</button>


<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<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">
<span id="modal-myvalue"></span> <span id="modal-myvar"></span> <span id="modal-bb"></span>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
</body>

<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>

</html>

最佳答案

是的,可以通过 Bootstrap3 借助预定义的模态方法来实现,例如 $('#myModal').on('show.bs.modal', function(e) {...})
文件:https://getbootstrap.com/docs/3.3/javascript/#modals-events

我希望下面的代码片段对您有很大帮助。

// data-* attributes to scan when populating modal values
var attributesArray = ['myvalue', 'myvar', 'bb'];
// This event fires immediately when the show instance method is called.
$('#myModal').on('show.bs.modal', function(e) {
// Get modal id
var $target = e.target.id;
// Get target values of clicked button
var $relatedTarget = e.relatedTarget;
// Iterate over each possible data-* attribute
attributesArray.forEach(function(attributeName){
$('#'+$target+ ' #modal-'+attributeName).text($($relatedTarget).data(attributeName) || '');
});
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">

<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>

<br><br>
<div class="container">
<div class="row">
<div class="col-sm-12">
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal" data-myvalue="trouducul" data-myvar="bisbis">
Launch demo modal 1
</button>
<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal" data-myvalue="troudbal" data-bb="troudbal">
Launch demo modal 2
</button>
</div>
</div>
</div>

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<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">
<h4>
<span id="modal-myvalue"></span>
<span id="modal-myvar"></span>
<span id="modal-bb"></span>
</h4>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>

关于jquery - 如何在 Bootstrap 3 中将变量传递给模态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60369193/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com