如何存储动态生成的输入和选择字段的值

时间:2012-09-24 07:07:28

标签: php jquery html dom

我正在创建一个页面,用户可以通过点击addrow和delrow按钮来添加或删除行,它正常工作。现在的问题是我想使用post方法存储动态生成的字段的值。这是代码..         function addRow(tableID){

        var table = document.getElementById(tableID);
        //var cargo_id = new Array();
        //var quantity_id = new Array();
        var rowCount = table.rows.length;
        var row = table.insertRow(rowCount);

        //var i = <?php// echo $index; ?>;
        //var name = "cargo_quantity" + i;

        var cell1 = row.insertCell(0);
        var element1 = document.createElement("input");
        element1.type = "checkbox";
        cell1.appendChild(element1);

        var cell2 = row.insertCell(1);
        cell2.innerHTML = rowCount++;

        var nameCount = rowCount-1;

        //quantity cell
        var cell3 = row.insertCell(2);
        var element3 = document.createElement("input");
        element3.type = "text";

        element3.name = "cargo_quantity"+nameCount;
        element3.id =  "cargo_quantity"+nameCount;

        //element3.setAttribute("value","");
        //element3.value = "<?//=(isset($row->cargo_quantity))?$row->cargo_quantity:''?>"
        cell3.appendChild(element3);


        //cargo type cell
        var cell4 = row.insertCell(3);
        var element4 = document.createElement("select");//name="cargo_type_id" id="cargo_type_id" field="List Cargo Type" class="border" style="width:150px;" >
       //name = "cargo_id" + i;
        element4.name = "cargo_id" + nameCount;
        element4.id = "cargo_id" + nameCount;
        element4.field = "List Cargo Type";

        element4.className = "border";





        //first option
        //create an option
        theOption=document.createElement("OPTION");
        //make some text
        theText=document.createTextNode("Cargo Type");
        //add the text to the option
        theOption.appendChild(theText);
        //add the option to the select
        element4.appendChild(theOption);
        theOption.setAttribute("selected","selected");


        <?php 
                      $pquery_pCargoType = "SELECT * FROM ".DB_PRE."tbl_cargo_type WHERE status='1' ";
                      $errNo_pCargoType = $con->DML_executeQry($pquery_pCargoType);
                      while($row_pCargoType = mysql_fetch_object($errNo_pCargoType)){
         ?>


          <option <?php if(isset($row->cargo_type_id) && $row->cargo_type_id==$row_pCargoType->cargo_type_id){?> selected="selected"<?php }?> value=""></option>

        //create an option
        theOption=document.createElement("OPTION");
        //make some text
        theText=document.createTextNode("<?php echo $row_pCargoType->name;?>");
        //add the text to the option
        theOption.appendChild(theText);
        //add the option to the select
        element4.appendChild(theOption);
        <?php if(isset($row->cargo_type_id) && $row->cargo_type_id==$row_pCargoType->cargo_type_id){?>
        theOption.setAttribute("selected","selected");
        <?php }?>

        theOption.setAttribute("value","<?php echo $row_pCargoType->cargo_type_id;?>");<?php } ?>


        //weight cell
        cell4.appendChild(element4);

        var cell5 = row.insertCell(4);
        var element5 = document.createElement("input");
        element5.type = "text";
        //name = "cargo_weight" + i;
        element5.name = "cargo_weight" + nameCount;
        element5.id = "cargo_weight" + nameCount;
        cell5.appendChild(element5);

        //Volume cell
        var cell6 = row.insertCell(5);
        var element6 = document.createElement("input");
        element6.type = "text";
        //name =  + i;
        element6.name = "cargo_volume" + nameCount;
        element6.id = "cargo_volume" + nameCount;
        cell6.appendChild(element6);         

    } 

    function deleteRow(tableID) {
        try {
        var table = document.getElementById(tableID);
        var rowCount = table.rows.length;

        for(var i=0; i<rowCount; i++) {
            var row = table.rows[i];
            var chkbox = row.cells[0].childNodes[0];
            if(null != chkbox && true == chkbox.checked) {
                table.deleteRow(i);
                rowCount--;
                i--;
            }


        }
        }catch(e) {
            alert(e);
        }
    }

`

<form method ="post">
<table id ="dataTable"  border="1">

                        <tr>
                        <td>Select </td>
                        <td>Sr.No.</td>
                        <td>Quantity</td>
                        <td>Type</td>
                        <td>Weight (KG per container)</td>
                        <td>Volume (cbm)</td>            
                        </tr>



                        <tr>
                        <td><input type="checkbox" name = "chk"</td>
                        <td>1</td>
                        <td><input type="text" field="Cargo Quantity" name="cargo_quantity1" id="cargo_quantity1" value="<?=(isset($row->cargo_quantity))?$row->cargo_quantity:''?>" style="width:150px;" class="border" /></td>



                        <td><select name="cargo_type_id" id="cargo_type_id" field="List Cargo Type" class="border" style="width:150px;" >
                        <option selected="selected" value="">Cargo Type</option>
                        <?php 
                          $pquery_pCargoType = "SELECT * FROM ".DB_PRE."tbl_cargo_type WHERE status='1' ";
                          $errNo_pCargoType = $con->DML_executeQry($pquery_pCargoType);
                          while($row_pCargoType = mysql_fetch_object($errNo_pCargoType)){
                        ?>

                        <option <?php if(isset($row->cargo_type_id) && $row->cargo_type_id==$row_pCargoType->cargo_type_id){?> selected="selected"<?php }?> value="<?php echo $row_pCargoType->cargo_type_id;?>"><?php echo $row_pCargoType->name;?></option>


                        <?php }?>
                        </select></td>
                        <td><input type="text" field="Cargo Weight" name="cargo_weight1" id="cargo_weight1" value="<?=(isset($row->cargo_weight))?$row->cargo_weight:''?>" style="width:150px;" class="border" /></td>


                  <td><input type="text" field="Cargo Volume" name="cargo_volume1" id="cargo_volume1" value="<?=(isset($row->cargo_volume))?$row->cargo_volume:''?>" style="width:150px;" class="border" /></td>            
                        </tr>
   </table>

</form>

1 个答案:

答案 0 :(得分:0)

您可以在json中存储关于字段名称的值,并且您可以通过迭代到json再次将值返回到您的字段