form.html 840 B

1234567891011121314151617181920212223242526272829303132
  1. <html>
  2. <head>
  3. <title>form - test</title>
  4. </head>
  5. <body>
  6. <h1>Form</h1>
  7. <form id="myform">
  8. <input id="myinput" type="text" name="q" value="foo"/>
  9. <button type="submit">submit</button>
  10. </form>
  11. <div id="value"></div>
  12. <script>
  13. function getParameterByName(name) {
  14. var url = window.location.href;
  15. name = name.replace(/[\[\]]/g, "\\$&");
  16. var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
  17. results = regex.exec(url);
  18. if (!results) return null;
  19. if (!results[2]) return '';
  20. return decodeURIComponent(results[2].replace(/\+/g, " "));
  21. }
  22. var q = getParameterByName('q');
  23. if (q) {
  24. document.querySelector('#value').innerHTML = q;
  25. }
  26. </script>
  27. </body>
  28. </html>