第二警報命令作品的代碼按預期(顯示元素「到」的值,但第一個警報命令不起作用(它應該做同樣的事情),這是爲什麼?使用document.getElementsByName()不起作用?
<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>
演示:HTTP ://jsfiddle.net/LrD9q/ – 2012-01-13 04:26:19