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

js 如何获取表单元素个数

WBOY
Release: 2016-06-01 09:55:00
Original
1796 people have browsed it
<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>
Copy after login

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

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template