在javascript中如何将两个文本框产生的随机数相加结果在第三个文本框中出现

如题所述

<!DOCTYPE html>
<html>
<head>
<title>Add</title>
</head>
<body>

<form>
<input type="button" value="rand" onclick="getRand()" /><!-- 生成随机数按钮 -->
<input id="num1" type="text" /><!-- 随机数1 -->
<input id="num2" type="text" /><!-- 随机数2 -->
<input type="button" value="add" onclick="add()" /><!-- 执行加操作 -->
<input id="result" type="text"/><!-- 显示结果 -->
</form>

<script type="text/javascript">
var num1 = document.getElementById("num1");
var num2 = document.getElementById("num2");
var result = document.getElementById("result");
function getRand(){
num1.value = Math.floor((Math.random() * 100000) + 1);
num2.value = Math.floor((Math.random() * 100000) + 1);
}

getRand();/*初始化*/

function add(){
result.value = new Number(num1.value) + new Number(num2.value);
}

</script>
</body>
</html>

温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答