私は JavaScript が初めてで、コードの一部について助けが必要です。ユーザーが数値を入力できるテキスト ボックスを作成すると、関数はその数のサイコロを転がします。また、制限を設定して、ユーザーが -10 または 100 を入力できないようにする必要もあります。これは、1 ~ 6 しかないためです。したがって、次のようになります。
var theInput = document.getElementById('num').value;
theInput = parseInt(theInput);
if (theInput < 1) {
theInput="1";
}
else if (theInput > 6) {
theInput = "6";
}
私が立ち往生している部分は、サイコロを振るために関数を実行するこのコードにテキスト ボックスをリンクする方法です。
<script type="text/javascript">
function SelectImage6() {
document.getElementById('outputDiv').innerHTML ='';
for(i=0; i<6; i++){
roll2 = Math.floor(Math.random() * 6) + 1;
imgName2 = '../images/die' + roll2 + '.gif';
document.getElementById('outputDiv').innerHTML +=
'<img alt="die image" src="' + imgName2+'" />';
}
}
</script>
<body>
<div style="text-align:center">
<input type="button" value="Click to Roll" onclick="SelectImage6();">
<p id="outputDiv">
<img id="dieImg2" alt="die image"
src="../images/die2.gif" >
</p>
</div>
コード内のどこに var theInput を割り当てますか? どんな助けでも大歓迎です。ありがとう!