Home  >  Article  >  Web Front-end  >  js 如何获取表单元素个数

js 如何获取表单元素个数

WBOY
WBOYOriginal
2016-06-01 09:55:001769browse
<code class="language-html">
    
    <script language="JavaScript">
    function showNumElements(){
         console.log("There are " + document.form1.length + " elements in this document");
    }
    </script>
    <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" onclick="showNumElements()">
    </form>
    
</code>

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

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn