Form

作者: JasonQiao | 来源:发表于2016-07-28 19:08 被阅读20次

forms 集合可返回对文档中所有 Form 对象的引用。

<html>
<body>

<form name="Form1"></form>
<form name="Form2"></form>
<form name="Form3"></form>

<script type="text/javascript">
document.write("This document contains: ")
document.write(document.forms.length + " forms.")
</script>

</body>

</html>

Form 对象代表一个 HTML 表单。

在 HTML 文档中 <form> 每出现一次,Form 对象就会被创建。

Form对象集合

elements[] 包含表单中所有元素的数组。元素在数组中出现的顺序和它们在表单的HTML 源代码中出现的顺序相同。每个元素都有一个 type 属性,其字符串值说明了元素的类型。使用方法formObject.elements[].property。
如果 elements[] 元素具有名称(input 标签的 id 或 name 属性),那么该元素的名称就是 formObject 的一个属性,因此可以使用名称而不是数字来引用 input 对象。举例,假设 x 是一个 form 对象,其中的一个 input 对象的名称是 fname,则可以使用 x.fname 来引用该对象。

<html>
<body>

<form id="myForm">
Firstname: <input id="fname" type="text" value="Mickey" />
Lastname: <input id="lname" type="text" value="Mouse" />
<input id="sub" type="button" value="Submit" />
</form>

<p>Get the value of all the elements in the form:<br />
<script type="text/javascript">
var x=document.getElementById("myForm");
for (var i=0;i<x.length;i++)
  {
  document.write(x.elements[i].value);
  document.write("<br />");
  document.write(x.elements[i].type);
  document.write("<br />");
  }
</script>
</p>

</body>
</html>

Form对象属性

acceptCharset
action
enctype
id
length
method
name
target

标准属性

className 设置或返回元素的class属性
...

Form 对象方法

reset() 重置
submit() 提交

Form 对象事件句柄

onreset
onsubmit

相关文章