form = window.document.forms[0]; for (var i = 0; i < form.elements.length; i++) {
if (form.elements[i].type = "text") {
alert("2");
form.elements[i].value = "helloworld";
} }
</script>
<title></title>
</head>
<body>
<form runat="server">
<input type="button" id="opw" onclick="openwindows()" value="打开" />
<input type="button" id="clw" onclick="closewindows()" value="关闭" />
<input type="text" name="tx1" id="tx1" />
<input type="text" name="tx2" id="tx2" />
</form>
</body>
</html>
刚学JS这为什么会报错 提示form is undefinde
if (form.elements[i].type = "text") {
alert("2");
form.elements[i].value = "helloworld";
} }
</script>
<title></title>
</head>
<body>
<form runat="server">
<input type="button" id="opw" onclick="openwindows()" value="打开" />
<input type="button" id="clw" onclick="closewindows()" value="关闭" />
<input type="text" name="tx1" id="tx1" />
<input type="text" name="tx2" id="tx2" />
</form>
</body>
</html>
刚学JS这为什么会报错 提示form is undefinde
或者把js 写到 onload 中获取 input 用 getElementsByTagName("input")
2. 在获取form时不建议这样:window.document.forms[0]
最好使用:document.getElementById('Form的ID').....