Tôi đã tự hỏi làm thế nào tôi có thể nhập một giá trị vào một biểu mẫu với html. Vì vậy, ngay khi người dùng truy cập vào trang web có số trong các hộp văn bản đã có và họ sử dụng các số đó để chơi và "trò chơi bổ sung". Đây là những gì tôi có cho đến thời điểm này:JavaScript - viết giá trị vào dạng html từ tập lệnh
<script type="text/javascript">
function addSubmit() {
//trying to have num1 and num2 write into the html form
var num1= Math.floor(89+Math.random()*11);
var num2 = Math.floor(89+Math.random()*11);
/*num1 = value1
num2 = value2*/
document.getElementById("value1").value = num1;
document.getElementById("value2").value = num2;
var guess = document.getElementById("submit").value;
var answer = num1 + num2;
//have to write this to main window
if (guess == answer)
document.writeln("Congratulations you answered correctly! The answer to: "+num1+"+"+num2+"= "+answer);
else
document.writeln("Nice try, but the correct answer to: "+num1+"+"+num2+"= "+answer);
}
function addGiveUp() {
var num1 = Math.floor(89+Math.random()*11)
var num2 = Math.floor(89+Math.random()*11)
document.addition.value1.value=num1;
document.addition.value2.value=num2;
var guess = document.getElementById("submit").value;
var answer = (document.getElementById("value1").value) + (document.getElementById("value2").value);
document.writeln("Never give up! The answer to: "+num1+"+"+num2+"= "+answer);
}
</script>
<form name="addition" action="" method="get">
<table border="1">
<tr>
<td>Value 1:</td> <td><input type="text" name="value1" id="value1"/></td>
</tr>
<tr>
<td>Value 2:</td> <td><input type="text" name="value2" id="value2"/></td>
</tr>
<tr>
<td>Answer:</td> <td><input type="text" id="answer" /></td>
</tr>
<tr><td><input type="button" value="Submit" onClick="addSubmit()" id="submit" /></td><td><input type="button" value="Give Up" onClick="addGiveUp()" /></td></tr>
</table>
</form>
Tôi đánh giá cao sự trợ giúp! Cảm ơn!
sự cố là gì? bạn bị kẹt ở đâu là bài tập về nhà này? bạn chỉ đang tìm kiếm đánh giá? Chào mừng bạn đến với SO – rlemon
Đó là bài tập về nhà. Tôi đang mắc kẹt tại cách viết các giá trị vào mẫu html tôi có trong mã. Tôi muốn sử dụng các mã javascript để viết vào mẫu w/o phải di chuyển các hình thức vào kịch bản và cảm ơn! – Gcap