查找表单中每个元素的值

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <form id="frm1" action="/demo/demo_form.asp">
  6. 名:<input type="text" name="fname" value="Bill"><br>
  7. 姓:<input type="text" name="lname" value="Gates"><br><br>
  8. <input type="submit" value="提交">
  9. </form>
  10.  
  11. <p>单击“试一试”以显示表单中每个元素的值。</p>
  12.  
  13. <button onclick="myFunction()">试一试</button>
  14.  
  15. <p id="demo"></p>
  16.  
  17. <script>
  18. function myFunction() {
  19. var x = document.getElementById("frm1");
  20. var text = "";
  21. var i;
  22. for (i = 0; i < x.length ;i++) {
  23. text += x.elements[i].value + "<br>";
  24. }
  25. document.getElementById("demo").innerHTML = text;
  26. }
  27. </script>
  28.  
  29. </body>
  30. </html>