我的javascript代码遇到了一些问题。我的代码是为了能够查看是否取消选中复选框,如果是,则它永远不会作为随机数出现。
<style>
.checkboxdiv {
display: block;
margin-left:20px;
color: #ddd;
}
</style>
<head>
<script type="text/javascript">
var lotto = function(){
var TotalMoney = 240;
var unsold = [];
var R1 = Math.floor((Math.random() * 24) + 1);
document.getElementById("1").innerHTML = R1;
var R2 = Math.floor((Math.random() * 24) + 1);
document.getElementById("2").innerHTML = R2;
if (R2 == R1) {
var R2 = Math.floor((Math.random() * 24) + 1);
document.getElementById("2").innerHTML = R2;
}
for (var i = 1; i < 25; i++) {
if (!document.getElementById('P' + i).checked) {
TotalMoney = TotalMoney - 10;
unsold.push(i);
}
}
for (var i = 0; i < unsold.length; i++) {
if (R1 == unsold[i]) {
var R1 = Math.floor((Math.random() * 24) + 1);
document.getElementById("1").innerHTML = R1;
}
}
for (var i = 0; i < unsold.length; i++) {
if (R2 == unsold[i]) {
var R2 = Math.floor((Math.random() * 24) + 1);
document.getElementById("2").innerHTML = R2;
}
}
document.getElementById('TotalMoney').innerHTML = TotalMoney;
var prize1 = TotalMoney * 0.5;
document.getElementById('money1').innerHTML = prize1;
var prize2 = TotalMoney * 0.2;
document.getElementById('money2').innerHTML = prize2;
var profit = TotalMoney * 0.3;
document.getElementById('prof').innerHTML = profit;
}
</script>
</head>
<body>
<div class="container">
<div class="sidebar1">
<div style="margin-top: 20px" class="checkboxdiv"><input type="checkbox" class="heckboxs" id="P1" checked="checked" />1</div>
<div class="checkboxdiv"><input type="checkbox" class="heckboxs" id="P2" checked="checked" />2</div>
<div class="checkboxdiv"><input type="checkbox" class="chckboxs" id="P3" checked="checked" />3</div>
<div class="checkboxdiv"><input type="checkbox" class="chckboxs" id="P4" checked="checked" />4</div>
<div class="checkboxdiv"><input type="checkbox" class="chckboxs" id="P5" checked="checked" />5</div>
<div class="checkboxdiv"><input type="checkbox" class="chckboxs" id="P6" checked="checked" />6</div>
<div class="checkboxdiv"><input type="checkbox" class="chckboxs" id="P7" checked="checked" />7</div>
<div class="checkboxdiv"><input type="checkbox" class="chckboxs" id="P8" checked="checked" />8</div>
<div class="checkboxdiv"><input type="checkbox" class="chckboxs" id="P9" checked="checked" />9</div>
<div class="checkboxdiv"><input type="checkbox" class="chckboxs" id="P10" checked="checked" />10</div>
<div class="checkboxdiv"><input type="checkbox" class="chckboxs" id="P11" checked="checked" />11</div>
<div class="checkboxdiv"><input type="checkbox" class="chckboxs" id="P12" checked="checked" />12</div>
<div class="checkboxdiv"><input type="checkbox" class="chckboxs" id="P13" checked="checked" />13</div>
<div class="checkboxdiv"><input type="checkbox" class="chckboxs" id="P14" checked="checked" />14</div>
<div class="checkboxdiv"><input type="checkbox" class="chckboxs" id="P15" checked="checked" />15</div>
<div class="checkboxdiv"><input type="checkbox" class="chckboxs" id="P16" checked="checked" />16</div>
<div class="checkboxdiv"><input type="checkbox" class="chckboxs" id="P17" checked="checked" />17</div>
<div class="checkboxdiv"><input type="checkbox" class="chckboxs" id="P18" checked="checked" />18</div>
<div class="checkboxdiv"><input type="checkbox" class="chckboxs" id="P19" checked="checked" />19</div>
<div class="checkboxdiv"><input type="checkbox" class="chckboxs" id="P20" checked="checked" />20</div>
<div class="checkboxdiv"><input type="checkbox" class="chckboxs" id="P21" checked="checked" />21</div>
<div class="checkboxdiv"><input type="checkbox" class="chckboxs" id="P22" checked="checked" />22</div>
<div class="checkboxdiv"><input type="checkbox" class="chckboxs" id="P23" checked="checked" />23</div>
<div class="checkboxdiv"><input type="checkbox" class="chckboxs" id="P24" checked="checked" />24</div>
<!-- end .sidebar1 --></div>
<div class="content">
<div style="margin: 0 auto"><p>Total Money: $<span id="TotalMoney"></span></p></div>
<div style="margin: 0 auto"><p>Profit: $<span id="prof"></span></p></div>
<div style="margin: 0 auto"><p>1st Place: <span id="1"></span> Prize Money: $<span id="money1"></span></p></div>
<div style="margin: 0 auto"><p>2nd Place: <span id="2"></span> Prize Money: $<span id="money2"></span></p></div>
<button style="margin-left: 20px" onclick="lotto()">Lotto!</button>
<span id="test"></span>
<!-- end .content --></div>
<!-- end .container --></div>
</body>
答案 0 :(得分:0)
那么如果选中它然后生成一个随机数?如果是这样,这将有效:http://jsfiddle.net/5zBg6/3/
var choices = [];
$("#run").click(function(){
choices = [];
$( "input:checkbox:checked" ).each(function( i,value ) {
choices.push($(this).attr('value'));
});
var randomItem = Math.ceil(Math.random()*choices.length)-1;
alert(choices[randomItem]);
});
更新以选择所选已检查项目的随机值。