九九の勉強
<html>
<head>
<title>九九の勉強</title>
<script language="JavaScript">
<!--
function start() {
rand1 = Math.floor(Math.random() * 9) + 1; //問題作成
rand2 = Math.floor(Math.random() * 9) + 1;
msg = rand1 + " × " + rand2;
document.form1.text1.value = msg; //問題表示
document.form1.text2.value = ""; //回答入力欄をクリア
document.form1.text2.focus(); //カーソルを入力欄に移動
}
function check() {
if(document.form1.text2.value == (rand1 * rand2)) {
msg = "○ 正解"; //正解のメッセージ
}
else { //不正解のメッセージ
msg = "× 不正解(答え:" + rand1 * rand2 + ")";
}
alert(msg); //メッセージ表示
start(); //問題作成へ
return false; //フォーム送信キャンセル
}
//-->
</script>
<style>
<!--
body, input {
font-size: 20px;
text-align: center;
}
//-->
</style>
</head>
<body onLoad="start()">
<form action="" method="" name="form1" onSubmit="return check()">
<input type="text" name="text1" size="10"> = <input type="text" name="text2" size="3">
<input type="submit" value="OK">
</form>
</body>
</html>
〔 実行する 〕