如何从输入复选框获取值到模态引导程序?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Get Values of Selected Checboxes</title>
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("button").click(function(){
var favorite = [];
$.each($("input[name='sport']:checked"), function(){
favorite.push($(this).val());
});
alert("My favourite sports are: " + favorite.join(", "));
});
});
</script>
</head>
<body>
<form>
<h3>Select your favorite sports:</h3>
<label><input type="checkbox" value="football" name="sport"> Football</label>
<label><input type="checkbox" value="baseball" name="sport"> Baseball</label>
<label><input type="checkbox" value="cricket" name="sport"> Cricket</label>
<label><input type="checkbox" value="boxing" name="sport"> Boxing</label>
<label><input type="checkbox" value="racing" name="sport"> Racing</label>
<label><input type="checkbox" value="swimming" name="sport"> Swimming</label>
<br>
<button type="button">Get Values</button>
</form>
</body>
</html>
如何将警报更改为模态引导程序? Source Code
答案 0 :(得分:2)
$(document).ready(function() {
$("button").click(function() {
var favorite = [];
$.each($("input[name='sport']:checked"), function() {
favorite.push($(this).val());
});
$('#myModal').modal('show').on('shown.bs.modal', function() {
$("#checkid").html("My favourite sports are: " + favorite.join(", "));
});
});
});
模态HTML
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<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 id="checkid"></p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
答案 1 :(得分:0)
您可以使用以下内容轻松地将内容添加到引导模式的主体中:
modal.find('.modal-body').text('Your favourite sports are: ' + nameOfVariable);
您需要在调用模态时收集输入
$("#yourModal").on('show.bs.modal', function(){
var modal = $(this);
var favorite = [];
$.each($("input[name='sport']:checked"), function(){
favorite.push($(this).val());
});
var favorites = $.map(favorite, function(value){
return (value);
});
modal.find('.modal-body').text('My favourite sports are: ' + favorites.join(", "));
});
不要忘记添加更改按钮信息并添加模态(包括Bootstrap CSS和JS库)。
此处还有一个codepen,它将引导您完成HTML部分。
如果您仍需要帮助,请查看官方Bootstrap Modal文档。