尝试使用动态创建的输入元素序列化表单,但不发布元素值

时间:2014-10-31 03:16:15

标签: javascript jquery html forms dynamic

我正在动态添加元素。但是,当我尝试序列化表单时,没有动态生成的元素被序列化。

这是我用来向页面添加元素的函数:

function addObjects(IDname,classname)
{
    //to add more objects
    var number;
    switch(classname)
    {
        case "name": 
                    number = no_Name++;
                    break;
        case "part":    
                    number = no_part++;
                    break;                
    }
    var id = classname + number;

    $("#"+IDname).append('<tr class="'+id+'"><td><input id="'+id+'" class="'+id+'" type="text"> <button class="'+id+'" onclick=removeAdditions("'+id+'")>x</button></td></tr>');
}

页面如下所示:

<html>
<head>
    <script src="Controller.js" type="text/javascript"></script>
    <script type="text/javascript" src="https://ajax.microsoft.com/ajax/jQuery/jquery-1.4.2.min.js"></script>
    <script type="text/javascript">
        //in order to prevent form reload when button click occurs
    $(document).ready(function(){
        document.getElementById("ReportForm").onsubmit = function (event) { event.preventDefault(); }
  });
    </script>
</head>
<body>
<div class="detailsPane" id="detailsPane1" >
    <form id="ReportForm" name="ReportForm" >
      <table style="width: 100%;">
        <tbody>
                <tr>
                    <td>
                            1. Describe the Status briefly-
                    </td>
                    <td>
                            <textarea id="StatDescp" name="StatDescp"></textarea>
                    </td>
                </tr>
          </tbody>
        </table>
        <br>
        <table style="width: 100%;">
            <thead>
                <tr>
                    <th colspan="4" align="top">
                        Part Status
                    </th>
                </tr>
            </thead>
            <tbody>
                    <tr>
                        <td style="vertical-align:top;">
                            Part Name:
                        </td>
                        <td style="vertical-align:top;">
                            <table >
                                <tbody id="PartName">
                                    <tr class="partname0">
                                        <td><input class="part_name" type="text"> <button  onclick='addObjects("PartName","part_name");'>+</button></td>
                                    </tr>
                                </tbody>
                            </table>
                        </td>
            <tbody>
      </table>
    </form>
</div>
<div id="buttonDiv" >
    <a class="bottomLeftResultDiv" id="messageBox"></a>
    <input type="button" id="saveButton" value="Save" style="width:85px" onclick="save();" /> 
</div>
</body>
</html>

最后这里是保存按钮。

function save() {
    var select = document.getElementById('newReportPane');
    var contents = $('#ReportForm').serialize();
    contents = contents.replace(/=on/g, "=checked");
    contents = contents.replace(/\+/g, " ");
    $("#messageBox").html("Saving report...");
    console.log(contents);
    $.post("/Report/Report1", { action: "save", content: contents }, function (data) {
        if (data != "ACK")
            $("#messageBox").html("Unable to save.");
        else
            $("#messageBox").html("Report saved successfully");
    });
}

当我点击“保存”按钮时,它仅发布此StatDescp=而没有任何动态生成的元素。 我真的无法弄清楚为什么。 任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:3)

为每个添加的输入添加name=属性。

来自http://api.jquery.com/serialize/

  

要使表单元素的值包含在序列化字符串中,   元素必须具有name属性。

相关问题