W3School TIY Editor
W3School 在线教程
改变方向
暗黑模式
运行代码
<!DOCTYPE html> <html> <body> <h1>使用 document.forms 查找 HTML 元素</h1> <form id="frm1" action="/demo/html/action_page.php"> First name: <input type="text" name="fname" value="Bill"><br> Last name: <input type="text" name="lname" value="Gates"><br><br> <input type="submit" value="提交"> </form> <p>单击“试一试”按钮,显示表单中每个元素的值。</p> <button onclick="myFunction()">试一试</button> <p id="demo"></p> <script> function myFunction() { var x = document.forms["frm1"]; var text = ""; var i; for (i = 0; i < x.length ;i++) { text += x.elements[i].value + "<br>"; } document.getElementById("demo").innerHTML = text; } </script> </body> </html>