如果单击tr(td的x1(x2)),如何访问相同tr(x1)的td(x3)的值?

问题描述 投票:0回答:1

如果单击tr(td的x1(x2)),如何访问相同tr(x1)的td(x3)的值?

$(document).ready(function () {
        $.ajax({
                    url: '/api/Usuario/GetPermisosRolPorUsuario',
                    method: 'GET',
                    dataType:'JSON',
                    data: { NitEmpresa,NombreUsuario },
                    headers: {
                        'Authorization': 'Bearer '
                            + sessionStorage.getItem("accessToken")
                    },
                    success: function (data) {
                        debugger
                        $('#tblBody').empty();
                        $.each(data, function (index, value) {
                            var row =
                                $('<tr>'
                                    +'<td id="IdUsuario">'+ value.IdUsuario + '</td>'
                                    + '<td id="RolId">'   + value.RolId     + '</td>'
                                    + '<td id="Estado" >' + value.Estado + '</td>'
                                    + '<td>' + value.Rol + '</td>'
                                    + '<td>' + value.DescripcionRol + '</td>'
                                    + '<td>' + value.NombreUsuario + '</td>'
                                    + '<td>' + value.FullName + '</td>'
                                    + '<td>' + value.licenciaEmpresa + '</td>'
                                    + '<td>' + value.RazonSocial + '</td>'
                                    + '<td>' + value.NitEmpresa + '</td>'
                                    + '<td>' + value.Correo + '</td>'
                                    + '<td>' + value.Celular + '</td>'
                                    + '<td>' + formatDate(value.licenciaFechaExpire) + '</td>'
                                );
                            $('#tblData').append(row);
                        });
                         $('#tblBody tr').on('click', '#Estado', function ()
                         {
                                 var celda = $(this).text();

                             var Estado = 0;

var IdUsuario = $(this).find('td:first')。html()//这是不带'#Estado'的传统方式,但是我需要在不同的单元格中多次单击(td = 1 ,td = 2和td 3)在所有情况下都从tr捕获不同td的相同数据]

                             var IdUsuario = $(this).find('td:first').html()//This is the traditional way without '#Estado', but I need to put several clicks, in different cells (td = 1, td = 2 and td 3) in all cases capture the same data from a tr, from different td

                             var Estado1 = $(this).find('td:nth-child(3)').val();//This is the traditional way without '#Estado', but I need to put several clicks, in different cells (td = 1, td = 2 and td 3) in all cases capture the same data from a tr, from different td

                             //if (Estado1=='true') {
                             //    Estado = false
                             //} else if (Estado1 == 'false')
                             //{
                             //    Estado = 'true'
                             //}

                             alert(celda +'-'+IdUsuario+'-'+Estado1 ); //test returns is null +IdUsuario+'-'+Estado1 (celda=ok)

                             //debugger
                             //$.ajax({
                             //       type: "GET",
                             //       url: "/api/Usuario/CambiarEstadoPersona",
                             //       data: { Estado, IdUsuario },
                             //       dataType: 'JSON',
                             //       headers: {'Authorization': 'Bearer '+ sessionStorage.getItem("accessToken")},
                             //       success: function (response) {
                             //           if (response.Ok) {
                             //               toastr["success"](response.Message, "Atención!");
                             //           } else {
                             //               toastr["error"](response.Message, "Error");
                             //           };
                             //       },
                             //       error: function (jQXHR) {
                             //           toastr.error('Sistemas Infinitos Informa: '+jQXHR.responseText);
                             //       }

                             // });
                         });
                    },
                    error: function (jQXHR) {
                            toastr.error('Sistemas Infinitos Informa: '+jQXHR.responseText);
                    }
                });
    });
javascript jquery html html-table tr
1个回答
0
投票

首先,您需要发送整行,您可以执行以下操作:

<tr onclick="myFunction(this);">

之后,您可以通过列索引访问任何列。这是一个工作示例:

function rowData(row) {
  var entireRow = $(row).closest('tr').find('td');
  var col1 = entireRow.eq(0).text();
  var col2 = entireRow.eq(1).text();
  var col3 = entireRow.eq(2).text();
  
  alert("Col values: " + col1 + " " + col2 + " " + col3);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<table>
  <thead>
    <tr>
      <th>Col 1</th>
      <th>Col 2</th>
      <th>Col 3</th>
    </tr>
  </thead>
  <tbody>
    <tr onclick="rowData(this);">
      <td>1a</td>
      <td>2a</td>
      <td>3a</td>
    </tr>
    <tr onclick="rowData(this);">
      <td>1b</td>
      <td>2b</td>
      <td>3b</td>
    </tr>
  </tbody>
</table>

这样做,您无需指定任何行ID或列ID

© www.soinside.com 2019 - 2024. All rights reserved.