通过按Tab键动态添加行

时间:2017-01-03 07:56:54

标签: javascript php codeigniter

enter image description here如何通过在php中按Tab键向表中添加行。

嗨,我想通过在php中按Tab键向表中添加行,对于1s行,它的工作正常。但对于动态行,它无法正常工作。

   <form class="form-horizontal" role="form" id="default" method="post" action="<?php echo base_url('inventory_c/add_purchase/'.$this->session->userdata('cus_id'));?>">

                <div  style="width:100%; align:left; margin-bottom: 20px;">
                    <!--table class="table table-bordered"-->
                     <table class="table table-striped table-hover metable table-bordered" id="editable-sample">
                        <tr>
                            <th><input class='check_all' type='checkbox' onclick="select_all()"/></th>
                            <th>S.No</th>
                            <th>Item Name</th>
                            <th>Quantity</th>

                            <th >Vat %</th>
                            <th >Rate</th>
                            <th>Total</th>
                            <!--th>Action</th-->
                        </tr>
                        <tr>
                            <td><input type='checkbox' class='case'/></td>
                            <td><span id='snum'>1.</span></td>
                            <td><input class="form-control" type='text'    id='itemname_1' name='itemname[]' autofocus/>

                            </td>
                            <td><input class="form-control" type='text' id='quantity_no_1' name='quantity_no[]'/>
                            <input type='hidden' name='customer_id_1' id='customer_id_1' value='<?php echo $this->session->userdata('cus_id') ?>' class='form-control' />
                            <input class='form-control' type='hidden' id='supplier_id_1' name='supplier_id[]' />
                            <input type="hidden"  id="dummy_itemnum" />
                            <input type="hidden"  id="dummy_itemname" />
                            </td>


                            <td><input class="form-control" type='text' disabled  maxlength="200" id='vat_no_1' name='vat_no[]' value=""/></td>
                            <td><input class="form-control" type='text' disabled  maxlength="200" id='rate_no_1' name='rate_no[]'/> </td>
                            <td><input class="form-control" type='text' disabled id='total_no_1' name='total_no[]'/> </td>
                            <!--td id="hideAddRow">
                            <div id="hideAddRow">
                            <button type="button" class='btn btn-success addRow' id='addRow'>+ Add More</button>
                            </div>
                            </td-->
                            </tr>

                    </table>

                    <button type="button" class='btn btn-danger delete'>- Delete</button>
                    <button type="button" class='btn btn-success addmore'>+ Add More</button>

                    <section class="panel">
                        <input type="submit" class="finish btn btn-primary" value="Finish" name="submit" id="submit">
                    </section>

            </form>

我的脚本代码:

   $('#quantity_no_1').bind("keydown", function(event) {

   var keycode = (event.keyCode ? event.keyCode : (event.which ? event.which : event.charCode));
   if (keycode == 9) { 


      count=$('table tr').length;

      var data="<tr><td><input type='checkbox' class='case'/></td><td><span id='snum"+i+"'>"+count+".</span></td>";
data +="<td><input class='form-control' type='text' autofocus id='itemname_"+i+"' name='itemname[]'/></td> <td><input class='form-control' type='text' id='quantity_no_"+i+"' name='quantity_no[]'/><input type='hidden' name='customer_id' id='customer_id' value='<?php echo $this->session->userdata('cus_id') ?>' class='form-control' /><input class='form-control' type='hidden' id='supplier_id"+i+"' name='supplier_id' /><input  class='form-control' type='hidden'  id='dummy_itemnum_"+i+"' name='dummy_itemnum[]' /><input class='form-control' type='hidden' id='dummy_itemname_"+i+"' name='dummy_itemname[]' />  </td><td><input class='form-control' disabled type='text' id='vat_no_"+i+"' name='vat_no[]'/></td><td><input class='form-control' type='text' disabled id='rate_no_"+i+"' name='rate_no[]'/></td><td><input class='form-control' disabled type='text' id='total_no_"+i+"' name='total_no[]'/></td></tr>";

$('table').append(data);
row = i;

$('#itemname_'+i).autocomplete({

source: function( request, response ) {

    $.ajax({

                //type: "POST",
                url: "product_autocomplete",
                dataType: "json",
                //cache: false,
                method: 'post',
                //data: 'name_startsWith='+request.term+'&type='+'country_table'+'&row_num='+row,
                data: {
                   name_startsWith: request.term,
                   type: 'country_table',
                   row_num : row
                },

             success: function( data ) 
               {
                 response( $.map( data, function( item )
                  {
                    var code = item.split("|");
                        return {
                        label: code[0],
                        value: code[0],
                        data : item
                              }
                  }));
               }


               }); 

},
autoFocus: true,            
minLength: 0,
select: function( event, ui ) {
    var names = ui.item.data.split("|");
    id_arr = $(this).attr('id');
    id = id_arr.split("_");                 
    //$('#quantity_no_'+id[1]).val(names[1]);
    var al = $('#vat_no_'+id[1]).val(names[2]);
    //alert(al);
    $('#rate_no_'+id[1]).val(names[3]);
    $('#supplier_id'+id[1]).val(names[4]);
}   
});


    $('#quantity_no_'+i).change(function(){

var icon = $('#itemname_'+id[1]).val();
var icon1 = icon.split('-');
 $('#dummy_itemnum_'+id[1]).val(icon1[0]);
  $('#dummy_itemname_'+id[1]).val(icon1[1]);
var itemnum = $('#dummy_itemnum_'+id[1]).val();
var itemname = $('#dummy_itemname_'+id[1]).val();
var quantity = $('#quantity_no_'+id[1]).val(); 
var rate_no = $('#rate_no_'+id[1]).val();
var vat_no = $('#vat_no_'+id[1]).val();
var sales = parseInt(rate_no*quantity);
$('#total_no_'+id[1]).val(sales);
var t1 = $('#total_no_1').val();
//alert(t1);
var t2 = $('#total_no_'+id[1]).val();
var a = parseInt(t1);
var b = parseInt(t2);
var c  = a+b; 

$('#total_amt').val(c);

var t3 = parseFloat(t1+t2);

var supplier_id = $('#supplier_id'+id[1]).val();
var customer_id = $('#customer_id').val();

$.ajax({

                type: "POST",
                url: "add_temp_purchase",
                cache: false,
                data: 'itemnum='+itemnum+'&itemname='+itemname+'&costprice='+rate_no+'&quantity='+quantity+'&customer_id='+customer_id+'&sales='+sales+'&supplier_id='+supplier_id,
                dataType: "html",
                success: function(returnhtml) {

                }

                });

});


i++;


}
  [enter image description here][1]});

0 个答案:

没有答案