<html>
    <body>
    <  language=\" \">
    function showNumElements(){
         console.log(\"There are \" + document.form1.length + \" elements in this document\");
    }
    </ >
    <form name=\"form1\">
        Enter First Name:
        <input type=\"text\" size=15><br>
        Enter Last Name:
        <input type=\"text\" size=20><br>
        Enter address:
        <input type=\"text\" size=40><br><br>
        <input type=\"button\" value=\"Submit\"  =showNumElements()>
    </form>
    </body>
</html>

首先获取表单对象,然后通过length属性获取表单元素的个数。输出结果为:“There are 4 elements in this document”。

你可以把代码复制到这个页面运行一下。

收藏 打印