>  기사  >  웹 프론트엔드  >  js 获取页面表单的个数

js 获取页面表单的个数

WBOY
WBOY원래의
2016-06-01 09:55:001307검색
<code class="language-html">

<form name="form1" method="post" action="">
    <input type="text" name="textfield">
</form>
<form name="form2" method="post" action="">
    <input type="button" value="Only a sample button" onclick="return false"> 
</form>
<form name="form3" method="post" action="">
    <input type="checkbox" name="checkbox" value="checkbox">
</form>
<button onclick="function1()">Click here</button>
<script language="JavaScript">
function function1() {
    var m = document.forms.length;
    console.log("There are "+m+" forms in this document") 
} 
</script>

</code>

本实例中,我们创建了3个form表单,然后使用js的document.forms.length来获取表单个数,最后在控制台中输出这个值。最终输出结果为“There are 3 forms in this document”。

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

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.