0

ユーザーが行を追加または削除できるページを作成しています。行を追加および削除ボタンをクリックすると、正常に機能します。ここで問題となるのは、postメソッドを使用して動的に生成されたフィールドの値を保存したいということです。`ここにコードがあります。functionaddRow(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>
4

1 に答える 1

0

fieldNameに関してjsonに値を格納でき、jsonを反復処理することでypurフィールドに値を戻すことができます。

于 2012-09-24T07:10:15.900 に答える