hw08_11_!Form.html 927 B

12345678910111213141516171819202122232425262728293031323334
  1. <header>
  2. <h1>Form</h1>
  3. </header>
  4. <body>
  5. <script>
  6. obj = {
  7. "Name": "chevrolet chevelle malibu",
  8. "Cylinders": 8,
  9. "Displacement": 307,
  10. "Horsepower": 130,
  11. "Weight_in_lbs": 3504,
  12. "Origin": "USA",
  13. "in_production": false
  14. };
  15. const createForm = () => {
  16. const typesMap = { 'boolean': 'checkbox', 'number': 'number', 'string': 'text' };
  17. let str = "<form>\n";
  18. for (var name in obj) {
  19. value = obj[name];
  20. type = typeof value;
  21. htmlType = typesMap[type];
  22. str += ` <label>${name}: <input type="${htmlType}" value="${value}" /></label>\n`;
  23. }
  24. str += "</form>";
  25. return str;
  26. }
  27. str = createForm(obj);
  28. document.write(str);
  29. alert(str);
  30. </script>
  31. </body>