<! DOCTYPE html>
<meta charset= "UTF-8" >
<title> generate forms based on data </title>
<script>
Defines an array to undertake the contents of the object inside
Extracts an element in an array (this element is an object)
Use the Element object Type property to determine what type of label this object is.
And then the object gets the property value. Attach attribute to tag
Old routines
Window.onload = function () {
var arr = [{
Label: "User Name",
Name: "Username",
Type: "Text"
}, {
Label: "Password",
Name: "Password",
Type: "Password"
}, {
Label: "Gender",
Name: "Gender",
Type: "Select",
Value: [' Male ', ' female ']
}, {
Label: ' Is it about? '
Name: ' Arrange ',
Type: ' Radio ',
Value: [' about ', ' not about ']
}, {
Label: "Hobby",
Name: "Hobby",
Type: "checkbox",
Value: [' Basketball ', ' Football ', ' Badminton ', ' ping pong ', ' Mountain climbing ', ' shopping ', ' travel ', ' see Beauty ']
}, {
Label: ' Are you married ',
Name: ' Married ',
Type: ' Radio ',
Value: [' married ', ' unmarried ']
}, {
Label: ' Introduction ',
Name: ' Resume ',
Type: ' TextArea '
}];
Generate forms based on data
Mesh: Generate HTML structure from data
1) Generate Select drop-down box ==> <label>xx</label><select><option></option></select>
2) checkbox/radio ==> <label>xx</label><label><input type = "Checkbox/radio" > Basketball </ Label>
3) Other conditions ==><label>xx</label><input type = "Text" >
4) Create multiline text box ==><label>xx</label><textarea></textarea>
Traverse data to generate different HTML structures depending on the type
for (var i = 0; i < arr.length; i++) {
var p = document.createelement ("P");
Generate label label
Each form element will have a label label that needs to be generated by the public without being placed in the judging condition of the execution body.
var label = document.createelement ("label");
Label the contents of the package
label.innerhtml = Arr[i].label;
Add a for property for a label
Label.for = Arr[i].name;
declaring variables
var input;
Judge the particularity first
Switch (arr[i].type) {
Select
Case ' SELECT ':
input = document.createelement ("select");
Input.id = Arr[i].name;
Input.name = Arr[i].name;
Generate drop-down options
for (var j = 0; J < Arr[i].value.length; J + +) {
var opt = document.createelement ("option");
Option be sure to add the Value property, which is the option content that feeds back to the server
Opt.value = Arr[i].value[j];
This is the content of option package
opt.innerhtml = Arr[i].value[j];
Put option in the Select
Input.appendchild (opt);
}
Break
TextArea
Case ' textarea ':
input = document.createelement ("textarea");
Input.id = Arr[i].name;
Input.name = Arr[i].name;
Break
Checkbox/radio
Case ' checkbox ':
Case ' Radio ':
<label>xx</label><label><input type = "Checkbox/radio" > Basketball </label>
Create a span tag that wraps all the
input = document.createelement ("span");
Loop value value, put all the options generated by the HTML structure
for (var j = 0; J < Arr[i].value.length; J + +) {
var item = document.createelement ("label");
var checkbox = document.createelement ("input");
Set type
Checkbox.type = Arr[i].type;
Checkbox.name = Arr[i].name;
Item.appendchild (checkbox); <label><input type = "Checkbox/radio" ></label>
Set the text node
var txt = document.createtextnode (arr[i].value[j]);
Item.appendchild (TXT); <label><input type = "Checkbox/radio" > Basketball </label>
Input.appendchild (item);
}
Break
Text/password
Default
input = document.createelement ("input");
Input.tpye = Arr[i].type;
Input.id = Arr[i].name;
Input.name = Arr[i].name;
}
Put the label and the input tag inside the P
P.appendchild (label);
P.appendchild (input);
Put the P tag inside the document body
Document.body.appendChild (P);
}
}
</script>
<body>
<form>
</form>
</body>
Generate forms based on data