Form length eigenschap

定义和用法

length 属性返回表单中元素的数量。

实例

例子 1

返回表单中元素的数量:

var x = document.getElementById("myForm").length;

Try it yourself

例子 2

返回表单中每个元素的值:

var x = document.getElementById("myForm");
var txt = "";
var i;
for (i = 0; i < x.length; i++) {
  txt = txt + x.elements[i].value + "<br>";
}
document.getElementById("demo").innerHTML = txt;

Try it yourself

Syntax

formObject.length

Technical details

Return value: Number, indicating the number of elements in the form.

Browser support

The numbers in the table indicate the first browser version that fully supports this property.

Chrome Edge Firefox Safari Opera
Chrome Edge Firefox Safari Opera
Support Support Support Support Support