2017-03-17 69 views
0

我使用數據表jQuery來顯示錶中的數據,並向每行添加了一個按鈕<button>editar</button>,並且它在每次顯示數據和按鈕時工作正常行,但我想要檢索輸入類型中特定行的信息,問題是我不知道如何捕獲數據表中按鈕的事件點擊並檢索信息,請你幫助我,這是我到目前爲止的代碼:在輸入文本中檢索數據表jquery行的信息

 $(document).ready(function() { 
      $('#myTable').DataTable({ 
       searching: false, 
       paging: true, 
       responsive: true, 
       "ajax": { 
        "url": "/home/loaddata", 
        "type": "GET", 
        "datatype": "json" 
       }, 
       "columns": [ 
         { "data": "pais", "autoWidth": true, "orderable": true }, 
         { "data": "nombres", "autoWidth": true, "orderable": false }, 
         { "data": "telefono", "autoWidth": true, "orderable": false }, 
         { "data": "correo", "autoWidth": true, "orderable": false }, 
         { "data": "dui", "autoWidth": true, "orderable": false }, 
         { "data": "nit", "autoWidth": true, "orderable": false }, 
         { "defaultContent": "<button>editar</button>"} 
       ], 
       "oLanguage": { 
        "sEmptyTable": "No hay registros disponibles", 
        "sInfo": "Hay _TOTAL_ registros. Mostrando de (_START_ a _END_)", 
        "sLoadingRecords": "Por favor espera - Cargando...", 
        "sSearch": "Filtro:", 
        "sLengthMenu": "Mostrar _MENU_", 
        "oPaginate": { 
         "sLast": "Última página", 
         "sFirst": "Primera", 
         "sNext": "Siguiente", 
         "sPrevious": "Anterior" 
        } 
       } 

      }); 

      //This is the problem 
      $('#myTable tbody').on('click', 'button.editar', function() { 

        alert("you just pressed the button"); 

      }); 

     }); 
    </script> 

回答

1

使用下面的代碼:

$('#myTable tbody').on('click', 'button', function() { 
    var data = $('#myTable').DataTable().row($(this).closest('tr')).data(); 

    console.log(data, data['nombres']); 
});