javascript 在同一 HTML 页面的文本框字段中显示随机数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12814230/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Display random number in a textbox field in the same HTML page
提问by uday kurumana
I have code where I need to get a random number when I click the submit button. The random number should be displayed in the same page in another text box.
我有代码,当我点击提交按钮时,我需要获取一个随机数。随机数应显示在同一页面的另一个文本框中。
This is my code:
这是我的代码:
<html>
<head>
<script type="text/javascript">
function randomnumber(num1, num2)
{
num1 = parseInt(num1);
num2 = parseInt(num2);
if(num1 >= num2)
{
alert("Number 2 should be greater than Number 1");
}
else
{
var generator = Math.random()*(num2-num1);
generator = Math.round(num1+generator);
document.test.result.value = generator;
document.getElementById("p1").innerHTML=generator;
}
}
</script>
</head>
<body>
<form name="test" onsubmit = "return randomnumber(num1,num2)" >
<table>
<tr>
<td>
<input type="text" name="num1" size ="35" maxlength= "25">
</td>
<td>
<input type="text" name="num2" size ="35" maxlength= "25">
</td>
<td>
<input type="text" name="p1" value = "" size ="35" maxlength= "25">
</td>
<td>
<input type="submit" name="submit" value"generate">
</td>
</tr>
</table>
</form>
</body>
</html>
The random number is generated in the variable 'generator' in the script. I need to get the value in this textfield.
随机数在脚本中的变量“generator”中生成。我需要获取此文本字段中的值。
<td>
<input type="text" name="p1" value = "" size ="35" maxlength= "25">
</td>
回答by CrazyCasta
What you want is:
你想要的是:
var generator = Math.random()*(num2-num1);
generator = Math.round(num1+generator);
// document.test.result.value = generator; // Not sure what this line is for (don't see a test.result element.
document.getElementById("p1").value=generator;
Note .value
instead of .innerHTML
.
注意.value
而不是.innerHTML
。
回答by Alessandro Minoccheri
Is better in your html to change your input text in this mode:
最好在您的 html 中以这种模式更改您的输入文本:
<input type="text" name="p1" id="p1" value = "" size ="35" maxlength= "25">
and use this javascript code to change value:
并使用此 javascript 代码更改值:
document.getElementById("p1").value=generator;
Because the id is unique and you can select better your element
因为 id 是唯一的,您可以更好地选择您的元素
回答by Jacob George
This should do it
这应该做
document.getElementsByName('p1')[0].value = generator
The issue is "p1" is not the id of the textbox but the name. The above code will only work for the case of one element with the name "p1".
问题是“p1”不是文本框的 id,而是名称。上面的代码仅适用于名称为“p1”的元素的情况。
The following will work only if you use "p1" as the Id of your textbox.
仅当您使用“p1”作为文本框的 ID 时,以下内容才有效。
document.getElementById('p1').value = generator
Also, for textboxes, use .value
instead of .innerHTML
此外,对于文本框,使用.value
代替.innerHTML
回答by Jukka K. Korpela
There are multiple issues with the code (as well as the intended functionality, which looks like a homework assignment). To put text in the input field, it is best to use an id
attribute in the element, id="p1"
in this case, and assign to document.getElementById('p1').value
. Do not use form submission, since you are not submitting data to a server-side form handler. Instead, use <input type="button">
and an onclick
event handler in it. Check the input values for being numeric, at least by testing the result of parseInt()
, before doing any comparisons on them. Remove the assignment to document.test.result.value
, as its an erroneous reference and aborts the execution of the rest of the code. And check your HTML markup with a markup validator.
代码存在多个问题(以及预期的功能,看起来像是家庭作业)。要将文本放入输入字段,最好id
在元素中使用一个属性,id="p1"
在这种情况下,并分配给document.getElementById('p1').value
。不要使用表单提交,因为您没有向服务器端表单处理程序提交数据。相反,在其中使用<input type="button">
和onclick
事件处理程序。在parseInt()
对它们进行任何比较之前,至少通过测试 , 的结果来检查输入值是否为数字。删除对 的赋值document.test.result.value
,因为它是一个错误的引用并中止其余代码的执行。并使用标记验证器检查您的 HTML 标记。
回答by Senthil
what is result
结果是什么
document.test.result.value=generator;
document.test.result.value=生成器;
try name of the field p1.
尝试字段 p1 的名称。
document.test.p1.value=generator;
document.test.p1.value=生成器;