如果有人能告诉我如何使用JavaScript基于任何JSON对象动态生成HTML表单,我将为您提供帮助。
try this seee
You can use an existing library for that.
A simple one is: Angular Dynamic Forms
More standard way is the JSON Schema with this nice implementation Angular Schema Form . See examples on how it works here DEMO
HTML:
<html>
<head></head>
<body>
<body>
</html>
javascript:
<script>
//create a form
var f = document.createElement("form");
f.setAttribute('method',"post");
f.setAttribute('action',"submit.php");
//create input element
var i = document.createElement("input");
i.type = "text";
i.name = "user_name";
i.id = "user_name1";
//create a checkbox
var c = document.createElement("input");
c.type = "checkbox";
c.id = "checkbox1";
c.name = "check1";
//create a button
var s = document.createElement("input");
s.type = "submit";
s.value = "Submit";
// add all elements to the form
f.appendChild(i);
f.appendChild(c);
f.appendChild(s);
// add the form inside the body
$("body").append(f); //using jQuery or
document.getElementsByTagName('body')[0].appendChild(f); //pure javascript
</script>
you can create as many elements as you want dynamically.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.