在追加行和跨度后,select2无法正常工作,每添加一次新行都会加倍

时间:2019-04-24 03:41:51

标签: jquery append jquery-select2

我尝试追加行,然后select2不能正常工作,因为跨度类select2-container-默认每次添加新行都会加倍,并且我无法选择选项值,有人可以帮忙吗?

<script type="text/javascript">

$(document).ready(function(){

  //add row
  $('#addRow').on('click',function(){
    var lastRow = $('#dataTable tbody tr:last').html();
    $('#dataTable tbody').append('<tr>' + lastRow + '</tr>');
    $('#dataTable tbody tr:last input').val('');

    $('.select2:last').select2({
      placeholder : "-Pilih Product-",
      ajax:{
        type : "GET",
        url : "<?php echo site_url('quotation/get_product')?>",
        delay : 250,
        dataType : "json",
        data : function(params){
          return{
            name : params.term
          }
        },
        processResults : function(data,page){
          var results = [0];

          $.each(data,function(index,items){
            results.push({
              id:items.item_code,
              text:items.item_name,
            });
          });
          return {
            results:results
          };
        },
      }
    });

    $('[data-toggle="tooltip"].form-control').tooltip();  
    $('.prize').autoNumeric();
  });

  // Delete selected checkbox in the table
$('#delRow').click(function() {
    var lenRow    = $('#dataTable tbody tr').length;
    var lenChecked  = $("#dataTable input[type='checkbox']:checked").length;
    var row = $("#dataTable tbody input[type='checkbox']:checked").parent().parent();
    if (lenRow == 1 || lenRow <= 1 || lenChecked >= lenRow) {
        alert("Can't remove all row!");
    } else {
        row.remove();
    }
  });

});

0 个答案:

没有答案