简体   繁体   English

如何在Json创建的每个div内追加输入

[英]How to append an input inside each div that created from Json

i have a json and i create a simple dynamic form based on my json file . 我有一个json,我根据json文件创建了一个简单的动态表单。 there are labels and each lables are exist in divs seperately.but there is a problem in this code that this code does not show my inputs. 有标签,并且每个标签分别存在于div中。但是此代码中存在一个问题,该代码未显示我的输入。 how to appends them inside each divs below each lable? 如何将它们附加在每个标签下面的每个div内?

this code should show input but does not work. 此代码应显示输入,但不起作用。

here is my code : 这是我的代码:

 document.body.onload = addElement; function addElement() { var schema = [{ "queestion": "Name", "type": "128", "attrs": [{ "attr": { "name": "class", "value": "nofilling2 engword" } }, { "attr": { "name": "id", "value": "name" } }, { "attr": { "name": "type", "value": "text" } }, { "attr": { "name": "placeholder", "value": "Name" } }, { "attr": { "name": "name", "value": "_root.passengerinfo__1.passengerinfo.fullname.firstname" } } ] }, { "queestion": "Family", "type": "128", "attrs": [{ "attr": { "name": "class", "value": "nofilling2 engword" } }, { "attr": { "name": "id", "value": "family" } }, { "attr": { "name": "type", "value": "text" } }, { "attr": { "name": "placeholder", "value": "Family" } }, { "attr": { "name": "name", "value": "_root.passengerinfo__1.passengerinfo.fullname.lastname" } } ] } ] for (var i = 0; i < schema.length; i++) { var type = schema[i].type; if (type == 128) { var titleinput = schema[i].queestion; var newDiv = document.createElement("div"); newDiv.className = 'c-infoo'; var newContent = document.createElement('label'); newContent.className = 'abs-tlt'; newDiv.appendChild(newContent); newContent.innerHTML = titleinput + " : "; document.getElementById('tblreserve').appendChild(newDiv); var string = "<input "; for (var y = 0; y < schema[i].attrs.length; y++) { string += schema[i].attrs[y].attr.name + '="' + schema[i].attrs[y].attr.value + '" ' } string += ">"; newDiv.appendChild = string; } } } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script> <div id="tblreserve"></div> 

In your last line you wrote newDiv.appendChild=string; 在最后一行中,您编写了newDiv.appendChild=string; .

But string as its name says is a String and can't be appended as an child. 但是string顾名思义是String,不能作为子项追加。

You can use your string by writing newDiv.innerHTML+=string; 您可以使用string写入newDiv.innerHTML+=string; instead. 代替。

PS: Please fix your indentation style. PS:请修正您的缩进样式。 It doesn't look very nice... 看起来不太好...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM