动态表和值

时间:2012-09-21 08:20:52

标签: php

JQUERY

 <script type="text/javascript" src="jquery-1.5.1.min.js"></script>
    <script type="text/javascript">
            $('#aggiungi').live('click', function(){
            var thisRow = $(this).parent().parent();

            // clone and add data
            thisRow.clone(true).insertAfter(thisRow).data('is_clone',true);
            $(this).val("-");
            $(this).attr("id","remove");

            var nextRow = thisRow.next();
            nextRow.find('input:not(#aggiungi)').val("");

            if (thisRow.data('is_clone')){
                while(thisRow.data('is_clone')){
                    thisRow = thisRow.prev();
                }
            }else{
                nextRow.children(":first").remove();
            }

            currRowSpan = thisRow.children(":first").attr("rowspan");
            thisRow.children(":first").attr("rowspan", currRowSpan+1);
        });

        $('#remove').live('click', function(){
            var thisRow = $(this).parent().parent(),
                prevRow = thisRow.prev();

            if (thisRow.data('is_clone')){
                while(prevRow.data('is_clone')){
                    prevRow = prevRow.prev();
                }
            }else{
                prevRow = thisRow.next()
                                 .removeData('is_clone')
                                 .children(":first")
                                 .before(thisRow.children(":first"))
                                 .end();
            }

            currRowSpan = prevRow.children(":first").attr("rowspan");
            prevRow.children(":first").attr("rowspan", currRowSpan-1);
            thisRow.remove();
        });


    </script>

PHP

        <form action="grading.php" method="post">              
                              <table width-"100%" id="tableRealizzazione">
                                <tr>
                                    <th></th>
                                    <th style="padding:12px;text-align:center;">Personale</th>
                                    <th style="padding:12px;text-align:center;">Percentage</th>
                                    <th style="padding:12px;text-align:center;">Marketing point</th>
                                    <th style="padding:12px;text-align:center;">Add/Remove</th>
                                </tr>'; 

    echo '<tr class="even">
                                          <td></td>
                                          <td style="padding:12px;"><input type="text" value="" id="Personale" name="Personale"></td>
                                          <td style="padding:12px;">
                                            <input type="text" id="from" name="from" size="5%"> -
                                            <input type="text" id="to" name="to" size="5%"> %
                                          </td>
                                          <td style="padding:12px;"><input type="text" class="totaliCostiGestione" id="marketpt" name="marketpt"></td>
                                          <td style="padding:12px;"><input type="text" name="programid" value ="34" size="10%"></td>
                                          <td style="padding:12px;"><input type="button" value="+" id="aggiungi"/></td>';         
                                   echo '</tr>';
 echo '<tr><td><input type="submit" name="submit" value="submit"></td></tr>';                      
                 echo '</table>
               </form>

当我填写所有文本框的值并单击“提交”时,它仅打印最后一行,最后一行的值为34,不会重复所有其余行。

这是我在小提琴中的代码 http://jsfiddle.net/gansai/PA9JF/

1 个答案:

答案 0 :(得分:1)

这是因为您必须从表单中创建数组:

例如:

<input type="text" id="from" name="from[]" size="5%"> -

克隆您必须添加的值:

newRow = thisRow.clone(true).insertAfter(thisRow).data('is_clone',true);
$(newRow+"[name^=programid]").val($(thisRow+"[name^=programid]").val());

喜欢这个JSFiddle

然后在PHP中:

foreach($_POST['marketpt'] as $num => $val){
    $Personale = $_POST[$num]['Personale'];
    $from = $_POST[$num]['from'];
    $to = $_POST[$num]['to'];
    $marketpt = $_POST[$num]['marketpt'];
    $programid = $_POST[$num]['programid'];
    // process variables, eg: insert in database
}