<body>
<div class="container">
<div class="center">
<h1>Zeen Game</h1>
<div class="panel panel-default">
<div class="panel-body"><input type="text" id="oneA"> + <input type="text" id="oneB"> answer:<input type="text" id="ansone"><input type="button" value="check" onclick="checkAns('one')"/></div>
</div>
</div>
<script type="text/javascript">
document.getElementById("oneA").value=Math.floor(Math.random()*11+1);
document.getElementById("oneB").value=Math.floor(Math.random()*11+1);
function checkAns(grade){
var answer = Number(document.getElementById(grade+"A").value) +
Number(document.getElementById(grade+"B").value);
if (answer==Number(document.getElementById("ans"+grade).value)) {
alert("well done!");
} else {
alert("try again!");
}
}
</script>
</body>
我有两个随机数之和的功能“检查答案”。 我希望当前答案正确时,然后生成新的随机数,以制作新的和方程式。
答案 0 :(得分:0)
您必须将脚本的第一行添加到if
语句块中,并清除下一个等式的答案块,
像这样
<body>
<div class="container">
<div class="center">
<h1>Zeen Game</h1>
<div class="panel panel-default">
<div class="panel-body"><input type="text" id="oneA"> + <input type="text" id="oneB"> answer:<input type="text" id="ansone"><input type="button" value="check" onclick="checkAns('one')"/></div>
</div>
</div>
<script type="text/javascript">
document.getElementById("oneA").value=Math.floor(Math.random()*11+1);
document.getElementById("oneB").value=Math.floor(Math.random()*11+1);
function checkAns(grade){
var answer = Number(document.getElementById(grade+"A").value) +
Number(document.getElementById(grade+"B").value);
if (answer==Number(document.getElementById("ans"+grade).value)) {
alert("well done!");
document.getElementById("ans"+grade).value = '';
document.getElementById("oneA").value=Math.floor(Math.random()*11+1);
document.getElementById("oneB").value=Math.floor(Math.random()*11+1);
} else {
alert("try again!");
}
}
</script>
</body>
&#13;