第二个警报命令的代码按预期工作(显示元素"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) javascript ×1