如何调用添加在数据表行中的ID

时间:2019-02-04 06:11:06

标签: jquery json ajax datatable

我有一些模块,使用按钮来列出数据表中的所有客户详细信息。

问题:为什么我无法获得添加到该行的按钮的ID,当我单击该按钮时却没有警报显示发生这种情况的原因?

我在这里用ajax列出表中的所有客户

    $(document).ready(function(){
  $.ajax({
    url:'/logic_get_customer_data',
    type: 'GET',
    dataType: 'json',
    success:function(response) {

      var details = response.data;
      $.each(details, function (index, el) {

          var stringify = jQuery.parseJSON(JSON.stringify(el));

          var customer_name_each = stringify['customer_name'];
          var customer_address_each = stringify['customer_address'];
          var customer_email_each = stringify['customer_email'];
          var customer_number_each = stringify['customer_number'];
          var store_location_each = stringify['customer_location'];
          var customer_order_note_each = stringify['customer_order_note'];
          var customer_registered_each = stringify['customer_registered'];
          var customer_id_each = stringify['customer_id'];
          var action_each = '<button id="show_cart_button" class="btn btn-primary" type="button" value='+customer_id_each+' data-toggle="modal" data-target="#add_cart" ><i class="fas fa-cart-arrow-down"></i></button>';

          var t = $( "#tables" ).DataTable();
          t.row.add([customer_name_each,
            customer_address_each,
            customer_email_each,
            customer_number_each,
            store_location_each,
            customer_order_note_each,
            customer_registered_each,
            action_each]).draw();

        })
    }

  });
});

我的点击功能以调用ID

$("#show_cart_button").click(function () {
  var fired_button = $(this).val();
  alert('George');
});

1 个答案:

答案 0 :(得分:2)

因为它是在DOM之后添加的。使用on click函数,并针对ajax启动之前DOM中的内容。

$("body").on("click", "#show_cart_button",function () {
  var fired_button = $(this).val();
  alert('George');
});