我正在尝试让我的脚本允许用户在我的输入字段中选择他们想要掷骰子的数量(1-40),使用按钮onClick()函数捕获输入数字并显示/创建“骰子”div。
<script>
// ORIGINAL DICE ROLLER CODE, KEEP JUST IN CASE!!!//
function rollDice(){
var numDice = document.getElementById("diceNum").value;
var status = document.getElementById("status");
var d1 = Math.floor(Math.random() * 6) + 1;
var diceRoll = d1;
var container = document.getElementById("diceNum").value;
for (var i = 0; i < diceNum.value; i++){
//LOOP THROUGH APPHENDED INPUT VALUE AND POPULATE CONTAINER DIV//
container.innerHTML = '<div class="dice">+diceRoll+</div>';
};
numDice.innerHTML = diceRoll;
status.innerHTML = "You rolled "+diceRoll+".";+
};
// DICE ROLLER V(1.1 MOD) //
function showdice() {
var dicenum = prompt("how many dice would you like to roll?");
var diceint = Math.round(dicenum);
var diceroll, results = '';
if (diceint >= "50") {
alert("we don't have that many dice");
}
else if (diceint <= "0") {
alert("you need to roll at least one die");
}
else
for (i = 1; i <= diceint; i++) {
diceroll = Math.ceil(Math.random() * 6);
results += diceroll + ',';
}
alert(results);
}
// DICE ROLLER V(1.2 MOD) //
function showdice() {
var dicenum = prompt("how many dice would you like to roll?");
var diceint = Math.round(dicenum);
var results = [];
if (diceint >= "50") {
alert("we don't have that many dice");
}
else if (diceint <= "0") {
alert("you need to roll at least one die");
}
else
for (i = 0; i < diceint; i++) {
results[i] = Math.ceil(Math.random() * 6);
}
alert(results.join(', '));
}
</script>
</head>
<body>
<br/><br/><br/>
<form class="" action="index.html" method="post">
<h4>Choose number of dice to roll</h4>
<p>[1 - 40]</p>
<br/>
<input id="diceNum" type="text" name="DiceNumber" placeholder="">
<br/>
</form>
<br/><br/>
<div id="dieContainer" class="dice">0</div>
<br/><br/>
<button onclick="rollDice()" style="margin-left: 20px; margin-right: 20px;">Roll Dice</button>
<h2 id="status" style="clear:left;"></h2>
</body>
我添加了两个不同的解决方案,列为V(1.1)和V(1.2),因为我只想使用严格的JavaScript和HTML来实现这一点。
[编辑] 两个版本都经过测试,但我似乎无法正确使用逻辑,以使其中任何一个版本正常工作。
如何继续捕获输入值并创建/显示多个骰子div?
答案 0 :(得分:2)
原始尝试的简化版本:
function rollDice() {
var numDice = document.getElementById("diceNum").value;
var container = document.getElementById("dieContainer");
container.innerHTML = "";
for (var i = 0; i < numDice; i++) {
var diceRoll = Math.floor(Math.random() * 6) + 1;
container.innerHTML += '<div class="dice">' + diceRoll + '</div>';
};
};
<h4>Choose number of dice to roll</h4>
<input id="diceNum" type="text" value="3">
<button onclick="rollDice()">Roll Dice</button>
<div id="dieContainer" class="dice"></div>