前端双色球用模态框选球和中奖提示代码
时间: 2023-11-22 16:54:54 浏览: 205
以下是一个简单的前端双色球选球和中奖提示的示例代码,使用了Bootstrap框架的模态框组件:
HTML代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>双色球选球</title>
<!-- 引入Bootstrap CSS -->
<link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.6.0/css/bootstrap.min.css">
</head>
<body>
<div class="container mt-5">
<h1 class="text-center">双色球选球</h1>
<div class="row justify-content-center">
<button class="btn btn-primary mr-2" data-toggle="modal" data-target="#ballModal">选择红球</button>
<button class="btn btn-primary" data-toggle="modal" data-target="#ballModal" data-ball-type="blue">选择蓝球</button>
</div>
<div class="row justify-content-center mt-3">
<button class="btn btn-success" id="checkBtn">查看中奖结果</button>
</div>
</div>
<!-- 球号选择模态框 -->
<div class="modal fade" id="ballModal" tabindex="-1" role="dialog" aria-labelledby="ballModalLabel" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="ballModalLabel">请选择球号</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="row">
<!-- 生成33个红球 -->
<div class="col-12 mb-3">
<h4>红球</h4>
<div class="btn-group btn-group-toggle" data-toggle="buttons">
<?php for ($i = 1; $i <= 33; $i++): ?>
<label class="btn btn-outline-secondary ball-btn">
<input type="checkbox" autocomplete="off" value="<?= $i ?>"> <?= $i ?>
</label>
<?php endfor; ?>
</div>
</div>
<!-- 生成16个蓝球 -->
<div class="col-12">
<h4>蓝球</h4>
<div class="btn-group btn-group-toggle" data-toggle="buttons">
<?php for ($i = 1; $i <= 16; $i++): ?>
<label class="btn btn-outline-secondary ball-btn">
<input type="checkbox" autocomplete="off" value="<?= $i ?>"> <?= $i ?>
</label>
<?php endfor; ?>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button>
<button type="button" class="btn btn-primary" data-dismiss="modal">确定</button>
</div>
</div>
</div>
</div>
<!-- 中奖结果模态框 -->
<div class="modal fade" id="resultModal" tabindex="-1" role="dialog" aria-labelledby="resultModalLabel" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="resultModalLabel">中奖结果</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body" id="resultBody">
<p>请先选择球号并点击“查看中奖结果”按钮。</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button>
</div>
</div>
</div>
</div>
<!-- 引入jQuery和Bootstrap JS -->
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.slim.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.6.0/js/bootstrap.bundle.min.js"></script>
<script src="js/main.js"></script>
</body>
</html>
```
JS代码:
```javascript
// 定义数组变量存储选中的球号
var redBalls = [];
var blueBalls = [];
// 点击选择球号按钮时,设置模态框标题和提交按钮类型(红球或蓝球)
$('.btn[data-toggle="modal"]').on('click', function() {
var ballType = $(this).data('ball-type');
var modalTitle = ballType == 'blue' ? '请选择蓝球' : '请选择红球';
var submitBtnText = ballType == 'blue' ? '选择蓝球' : '选择红球';
$('#ballModalLabel').text(modalTitle);
$('.btn-primary[data-dismiss="modal"]').text(submitBtnText).data('ball-type', ballType);
// 清空选中的球号
if (ballType == 'blue') {
blueBalls = [];
} else {
redBalls = [];
}
$('.ball-btn.active').removeClass('active');
});
// 点击模态框提交按钮时,获取选中的球号并存储到数组变量中
$('.btn-primary[data-dismiss="modal"]').on('click', function() {
var ballType = $(this).data('ball-type');
var ballArr = ballType == 'blue' ? blueBalls : redBalls;
$('.ball-btn.active').each(function() {
ballArr.push($(this).find('input').val());
});
});
// 点击查看中奖结果按钮时,判断是否选择了足够的球号
$('#checkBtn').on('click', function() {
if (redBalls.length < 6 || blueBalls.length < 1) {
$('#resultBody').html('<p>请选择至少6个红球和1个蓝球。</p>');
} else {
// 生成随机中奖号码
var winningRedBalls = [];
var winningBlueBall = Math.floor(Math.random() * 16) + 1;
while (winningRedBalls.length < 6) {
var randomRedBall = Math.floor(Math.random() * 33) + 1;
if (winningRedBalls.indexOf(randomRedBall) == -1) {
winningRedBalls.push(randomRedBall);
}
}
// 计算中奖结果
var redMatchCount = 0;
for (var i = 0; i < redBalls.length; i++) {
if (winningRedBalls.indexOf(parseInt(redBalls[i])) != -1) {
redMatchCount++;
}
}
var blueMatchCount = blueBalls.indexOf(parseInt(winningBlueBall)) != -1 ? 1 : 0;
var prizeLevel = '';
if (redMatchCount == 6 && blueMatchCount == 1) {
prizeLevel = '一等奖';
} else if (redMatchCount == 6 && blueMatchCount == 0) {
prizeLevel = '二等奖';
} else if (redMatchCount == 5 && blueMatchCount == 1) {
prizeLevel = '三等奖';
} else if (redMatchCount == 5 && blueMatchCount == 0) {
prizeLevel = '四等奖';
} else if (redMatchCount == 4 && blueMatchCount == 1) {
prizeLevel = '五等奖';
} else if ((redMatchCount == 4 && blueMatchCount == 0) || (redMatchCount == 3 && blueMatchCount == 1)) {
prizeLevel = '六等奖';
} else {
prizeLevel = '未中奖';
}
// 显示中奖结果
var resultHtml = '<p>中奖号码:' + winningRedBalls.join(' ') + ' + ' + winningBlueBall + '</p>';
resultHtml += '<p>您选的号码:红球 ' + redBalls.join(' ') + ',蓝球 ' + blueBalls[0] + '</p>';
resultHtml += '<p>中奖等级:' + prizeLevel + '</p>';
$('#resultBody').html(resultHtml);
}
// 显示中奖结果模态框
$('#resultModal').modal('show');
});
// 点击球号按钮时,切换选中状态
$('.ball-btn').on('click', function() {
var isChecked = $(this).find('input').prop('checked');
if (isChecked) {
$(this).removeClass('active');
} else {
var ballType = $(this).closest('.modal-content').find('.btn-primary[data-dismiss="modal"]').data('ball-type');
var ballArr = ballType == 'blue' ? blueBalls : redBalls;
if (ballArr.length >= (ballType == 'blue' ? 1 : 6)) {
alert('您已选择足够的球号。');
return false;
}
$(this).addClass('active');
}
});
```
注意:上述代码中使用了PHP的for循环语句生成了球号选择的HTML代码,如果你不使用PHP,可以手动编写HTML代码。
阅读全文