Home >Web Front-end >JS Tutorial >Document.forms usage examples introduction_javascript skills
Overview
forms returns a collection (an HTMLCollection object) that contains all form elements in the current document.
Grammar
var collection = document.forms;
Example
Get form information
<script type="text/javascript"> $(function(){ var thisForm = document.forms['form1']; //获取name为form1的form表单 //或者 //var thisForm = document.forms[0]; //获取第一个form表单 console.info(thisForm.username.value); //输出表单name属性值为form1的 username的值 console.info(thisForm.address.value); document.forms[0].submit(); //表单提交 }) </script> <body> <!-- 以下为三个简单的form表单 --> <form action="x" name="form1" > <input type="text" name="username" value="zhangsan" /> <input type="text" name="address" value="beijing" /> </form> <form action="x" name="form2" > </form> <form action="x" name="form3" > </form> </body>