How to obtain the data attribute of an input located inside a td of a datatable using Jquery?

2

I have the following datatable:

 $(document).ready(function () {
    tabla_productos = $("#idtabla_productos").DataTable({
        "scrollX": true, "scrollY": "300px", "scrollCollapse": true, "searching": false, "paging": false, "ordering": false, "info": false, "autowidth": false
    });



<table id="idtabla_productos" class="table table-striped table-bordered nowrap" cellspacing="0" width="100%" style="margin-bottom: 0 !important;">
<thead style="background-color: rgba(34, 255, 0, 0.34);">
    <tr>

        <th style="text-align: right">Cantidad
        </th>            
    </tr>
</thead>
<tbody>
    <% int i = 1;
       foreach (var item in Model)
       { %>
    <tr >           
        <td>
            <input type="number" id="idcantidad_<%:i.ToString() %>"  class="fila_marcada" data-fila="<%:i%>" />
        </td>

    </tr>
    <% } %>
</tbody>
</table>

In the next event, I need to get the value of the row, which is defined in the input that is inside the td (data-row = <%: i% >):

$('#idtabla_productos tbody').on('keypress', 'tr', function (event) {
   //var fila=$(this).data('fila');si el data-fila estuviera en tr, obtendria el que está en el tr, pero en este caso necesito la fila(data-fila) del input que esta dentro del td

I've tried to do something like this:

 $(this).parents('td').data('fila');

   });
});

but it does not work for me, I would like to be able to get a solution, I would appreciate it very much please

    
asked by Danilo 06.04.2017 в 03:26
source

1 answer

4

If what you have is the <tr> you have to find the <input> in their descendants, this you can do with the function find

$('#idtabla_productos tbody').on('keypress', 'tr', function (event) {
  var data = $(this).find('input').data('fila');
  console.log(data);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="idtabla_productos">
<tbody>
  <tr><td><input id="1" data-fila="a" /></td></tr>
  <tr><td><input id="2" data-fila="b" /></td></tr>
  <tr><td><input id="3" data-fila="c" /></td></tr>
  <tr><td><input id="4" data-fila="d" /></td></tr>
  <tr><td><input id="5" data-fila="e" /></td></tr>
</tbody>
</table>
    
answered by 06.04.2017 / 03:38
source