Trying to serialize a form with dynamically generated input elements, but the element values ​​are not being published

I am adding items dynamically. However, when I try and serialize the form, none of the dynamically generated items are serialized.

This is the function I am using to add elements to the page:

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>');
}

      

The page looks like this:

<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>

      

And finally here is the save button.

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");
    });
}

      

When I click the Save button it only publishes this one StatDescp=

without any dynamically generated items. I really can't figure out why. Any help would be appreciated.

+3


source to share


1 answer


Give an attribute name=

for each input you add.

From http://api.jquery.com/serialize/



For a form element value to be included in a serialized string, the element must have a name attribute.

+3


source







All Articles