第二个警报命令的代码按预期工作(显示元素"to"的值,但第一个警告命令不起作用(它应该做同样的事情).为什么这个?
<html>
<head>
<script type="text/javascript">
function getValue()
{
alert(document.getElementsByName("to").value);
alert(document.forms[0].to.value);
}
</script>
</head>
<body>
<form>
<input name="to" type="hidden" value="hoolah" />
<input type="button" onclick="getValue()" value="Get Value!" />
<form/>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
Way*_*ett 17
getElementsByName返回一个HTMLCollection.您可以像这样访问第一个项目的值:
document.getElementsByName("to").item(0).value
Run Code Online (Sandbox Code Playgroud)
或者像这样:
document.getElementsByName("to")[0].value
Run Code Online (Sandbox Code Playgroud)
更多信息:
| 归档时间: |
|
| 查看次数: |
61452 次 |
| 最近记录: |