我如何写一个表格来从用户那里获取一个数字,并在该数字的范围内掷骰子,其中变量“输入”是他们的数字。我没有足够的表单经验,所有表单教程都是内联的。
<html>
<head>
</head>
<body>
<script>
result = Math.floor(((Math.random())* input))+1;
document.write(result);
</script>
</body>
</html>
我如何写一个表格来从用户那里获取一个数字,并在该数字的范围内掷骰子,其中变量“输入”是他们的数字。我没有足够的表单经验,所有表单教程都是内联的。
<html>
<head>
</head>
<body>
<script>
result = Math.floor(((Math.random())* input))+1;
document.write(result);
</script>
</body>
</html>
<html>
<head>
</head>
<body>
<form>
<input id="txtInput" type="TextBox" onkeyup="myFunc();" />
</form>
<script>
function myFunc(){
var input = document.getElementById("txtInput").value;
result = Math.floor(((Math.random())* input))+1;
document.write(result);
}
</script>
</body>
</html>
To achieve your goal and avoid document.write() that would erase everything, you can create a empty div, and then write the result to it. With this code you can repeat the operation of calling the random function as much times as you want:
<html>
<head>
</head>
<body>
n: <input type="text" id="n" name="n" value="">
<input type="submit" value="submit" onclick="call_result();">
<div id="result"></div>
<script type="text/javascript">
function call_result(){
var input = parseInt(document.getElementById("n").value);
var result = Math.floor((Math.random())*input)+1;
// document.write(result);
document.getElementById("result").innerHTML=result;
}
</script>
</body>
</html>