如何使用jquery add函数添加动态数据?

时间:2017-01-04 10:33:04

标签: javascript php jquery html

我有这个代码,我使用jQuery代码复制一个表,原始表工作正常与PHP提取查询,但复制过程不起作用,如果我添加PHP代码,但如果删除PHP代码它有效。

  <form action="" method="post" name="addusers" id="addusers">
            <div class="felements"><select name="proname" type="text" required class="tfield2" id="proname">
                <option id="0">-- Select Provider --</option>
                <?php $prov = $con->query("SELECT * FROM providers"); while ($vall = mysqli_fetch_array($prov)){ ?> 

                <option id="<?php echo $vall ['ID'];?>">
                    <?php echo $vall ['provname']?>
                </option>
                <?php } ?>
            </select></div>

            <div class="felements"><input name="invodate" type="text" required class="tfield" id="invodate" placeholder="Invoice Date"/>
                </div>
                    <div class="felements"><input name="Save" type="submit" class="button" value="Save" style="width: 91%;"></div>

        <table class="table table-borderd table-hover felements">
            <thead>
                <th>Company Name</th>
                <th>Claimed Amount</th>
                <th>Exchange Rate</th>
                <th>Claimed Amount (LYD)</th>
                <th>Insurance Type</th>
                <th><input type="button" value="+" id="add" class="btn btn-primary"></th>
            </thead>
            <tbody class="detail">
                <tr>
                    <td><select name="comname[]" type="text" class="form-control comname"><option id="0">-- Select Company --</option> 
                <?php $comp = $con->query("SELECT * FROM companys"); while ($vall = mysqli_fetch_array($comp)){?><option id="<?php echo $vall ['ID'];?>">           <?php echo $vall ['company']?></option><?php } ?></select></td>
                    <td><input name="claimed[]" type="text" class="form-control claimed" required placeholder="Invoice Amount"/></td>
                    <td><input name="rate[]" type="text" class="form-control rate" required placeholder="EX Rate"/></td>
                    <td><input name="netclaimed[]" type="text" class="form-control netclaimed" required placeholder="Invoice Amount"/></td>
                    <td><select name="reinsurer[]" type="text" class="form-control reinsurer">
                        <option id="0">-- Select Reinsurer --</option>
                        <option id="1">تكافلي</option>
                        <option id="2">تجاري</option>
                        </select></td>
                    <th><input type="button" value="-" id="remove" class="btn btn-primary"></th>
                </tr>
            </tbody>
            <tfoot>
                <th></th>
                <th></th>
                <th>Total</th>
                <th class="total">0</th>
                <th>LYD</th>
                <th></th>
            </tfoot>
        </table>

        </form>





    <?php require('../includes/footer.php'); ?>

    <script type="text/javascript">
        $(function(){
            $('#add').click(function(){
                addnewrow();
            });

            $('body').delegate('#remove','click',function(){
                $(this).parent().parent().remove();
            });

            $('.detail').delegate('.claimed, .rate', 'keyup',function(){
                var tr = $(this).parent().parent();
                var qty = tr.find('.claimed').val();
                var rate  = tr.find('.rate').val();
                var amt  = (qty * rate);
                tr.find('.netclaimed').val(amt);
                total();
        });

    });  

        function total(){
            var t = 0;
            $('.netclaimed').each(function(i,e) 
            {
                var amt = $(this).val()-0;
                t += amt;  
            }); 
            $('.total').html(t);
        }


        function addnewrow()
        {
            var tr = '<tr>'+
                     '<td><select name="comname[]" type="text" class="form-control comname"><option id="0">-- Select Company --</option>                                          <option id=""></option></select></td>'+
                     '<td><input name="claimed[]" type="text" class="form-control claimed" required placeholder="Invoice Amount"/></td>'+
                     '<td><input name="rate[]" type="text" class="form-control rate" required placeholder="EX Rate"/></td>'+
                     '<td><input name="netclaimed[]" type="text" class="form-control netclaimed" required placeholder="Invoice Amount"/></td>'+
                     '<td><select name="reinsurer[]" type="text" class="form-control reinsurer"><option id="0">-- Select Reinsurer --</option>       <option id="1">تكافلي</option><option id="2">تجاري</option></select></td>'+
                     '<th><input type="button" value="-" id="remove" class="btn btn-primary"></th>'+
                     '</tr>';
                $('.detail').append(tr);
            }
    </script>

1 个答案:

答案 0 :(得分:0)

如果选择不同,您需要AJAX内容或克隆而不是插入静态行:

function addnewrow() {
  $('.detail').append($("tbody.detail tr:eq(0)").clone());
}

FIDDLE