ホームページ  >  記事  >  ウェブフロントエンド  >  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 までご連絡ください。