Javascript 在DOM上动态创建元素,但窗体不发送任何内容

Javascript 在DOM上动态创建元素,但窗体不发送任何内容,javascript,html,dom,Javascript,Html,Dom,我有一个表单,它的一些字段是基于另一个字段动态添加的,我可以让它创建元素,但是当我发送表单时,这些字段都不会与页面加载时加载的非动态字段一起发送 下面是我如何添加的 function foo(field) { var selectionDiv = document.getElementById("div_" + field); var divLength = selectionDiv.children.length; for (var i = 0; i < divLength; i++)

我有一个表单,它的一些字段是基于另一个字段动态添加的,我可以让它创建元素,但是当我发送表单时,这些字段都不会与页面加载时加载的非动态字段一起发送

下面是我如何添加的

function foo(field) {
var selectionDiv = document.getElementById("div_" + field);

var divLength = selectionDiv.children.length;
for (var i = 0; i < divLength; i++) {
    if (selectionDiv.children[i].id.indexOf(field) != -1) {
        var element = document.createElement(selectionDiv.children[i].tagName);
        element.setAttribute("type", selectionDiv.children[i].type);
        element.setAttribute("value", selectionDiv.children[i].value);
        element.setAttribute("id", selectionDiv.children[i].id);
        if (selectionDiv.children[i].className != "") {
            element.setAttribute("class", selectionDiv.children[i].className);
        }
        $(element).appendTo("#div_" + field);
    }
}
}
函数foo(字段){
var-selectionDiv=document.getElementById(“div_”+字段);
var divLength=selectionDiv.childrence.length;
对于(变量i=0;i
插入后的HTML:

<form action="/URL" id="form" method="post" name="form" onsubmit="Confirmation('Are you sure?');return false;"><div class="form-horizontal">
<...OtherFields...>
<div class="form-group" id="form_group_test">
<label class="col-xs-2 control-label" for="Identity">test:</label>
<div class="col-xs-4" id="div_test">
        <input id="Fields_test__Identity" name="Fields[test].Identity" type="hidden" value="test">
        <input class="form-control hasTooltip" data-placement="right" data-toggle="tooltip" id="Fields_test__Value" name="Fields[test].Value" title="" type="text" value="*">
        <br>
        <button type="button" onclick="foo('test'); return false;" class="btn btn-default hasTooltip form-control" data_toggle="tooltip" data_placement="right" title=""><span class="glyphicon glyphicon-plus blue"></span></button>
        <input type="hidden" value="test" id="Fields_test1__Identity"><input type="text" value="*" id="Fields_test1__Value" class="form-control hasTooltip"></div>
<...MoreFields...>
</form>

测试:


有什么想法吗?

动态创建元素时,属性“name”丢失,请尝试以下代码:

function foo(field) {
var selectionDiv = document.getElementById("div_" + field);

var divLength = selectionDiv.children.length;
for (var i = 0; i < divLength; i++) {
    if (selectionDiv.children[i].id.indexOf(field) != -1) {
        var element = document.createElement(selectionDiv.children[i].tagName);
        element.setAttribute("type", selectionDiv.children[i].type);
        element.setAttribute("value", selectionDiv.children[i].value);
        element.setAttribute("id", selectionDiv.children[i].id);
        element.setAttribute("name", selectionDiv.children[i].id);
        if (selectionDiv.children[i].className != "") {
            element.setAttribute("class", selectionDiv.children[i].className);
        }
        $(element).appendTo("#div_" + field);
    }
}
}
函数foo(字段){
var-selectionDiv=document.getElementById(“div_”+字段);
var divLength=selectionDiv.childrence.length;
对于(变量i=0;i
您确定没有编辑掉“MoreFields”供参考的结束表单标记,
$(“”,selectionDiv.children[i])。附加到(“#div_uuz”+字段)可以为您完成大部分工作,而不是使用createElement和SetAttributence捕获!我认为id属性应该是enogh=)