简体   繁体   English

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

[英]How do I access the value of a td (x3) of the same tr (x1), if I click on the tr (x1 of td (x2))?

How do I access the value of a td (x3) of the same tr (x1), if I click on the tr (x1 of td (x2))?如果单击 tr(td (x2) 的 x1),如何访问相同 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);
            });

Thank you, I managed to access the brothers 'td', as follows:谢谢,兄弟'td'我成功访问了,如下:

                    $('tr td:nth-child(3)', '#tblData').click(function () {

returns to the father to look for his brothers回到父亲身边寻找他的兄弟

                            var $thisRow = $(this).closest('tr')

brothers td兄弟们

                            IdUsuario = $('td:first', $thisRow).text();
                            RolId = $('td:nth-child(2)', $thisRow).text();
                            Estado= $('td:nth-child(3)', $thisRow).text();

//an alert to print the values alert(IdUsuario + '-' + RolId + '-' + Estado); //打印值的警报 alert(IdUsuario + '-' + RolId + '-' + Estado);

            });
        },
        error: function (jQXHR) {
            toastr.error('Sistemas Infinitos Informa: '+jQXHR.responseText);
        }
    });
});
            $.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>'

First, each element in the DOM should have a unique id.首先,DOM 中的每个元素都应该有一个唯一的 id。 By repeating the same ID multiple times, I'm not sure your on('click') events will attach in all browsers and return the value you are looking for.通过多次重复相同的 ID,我不确定您的on('click')事件是否会附加到所有浏览器中并返回您正在寻找的值。 Instead, your click event should look something like this:相反,您的click事件应如下所示:

 $('tr', '#tblData').click(function () {
     var id1 = $('td:first-child', this).text();
     var id2 = $('td:nth-child(2)', this).text();
     var id3 = $('td:nth-child(3)', this).text();
     ...
 }

or if you only want to allow clicking on the first TD:或者如果您只想允许点击第一个 TD:

 $('tr td:first-child', '#tblData').click(function () {
     var $thisRow = $(this).closest('tr')
     var id1 = $(this).text();
     var id2 = $('td:nth-child(2)', $thisRow).text();
     var id3 = $('td:nth-child(3)', $thisRow).text();
     ...
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何添加 <td> 元素成一个 <tr> for循环中的元素? [jQuery] - How do I add 'x' number of <td> elements into a <tr> element in a for loop? [jQuery] 如何使用JavaScript中的循环函数声明变量x1,x2,…,x10? - How do you declare variables x1, x2,…,x10 using a loop function in JavaScript? JavaScript中逗号运算符的用途(x,x1,x2,...,xn) - The purpose of the comma operator in JavaScript (x, x1, x2, …, xn) 如何使用src(裁剪)编辑javascript图像对象的x1 y1 x2 y2 - How can i edit the x1 y1 x2 y2 of javascript image object with an src(cropping) jQuery:第一个tr少于X td - JQuery: first tr with less than X td 线的点x1,x2,y1,y2与径向树中的节点不一致 - the lines' points x1, x2, y1, y2 do not coincide with nodes in a radial tree 如何使用JavaScript从地图协调中心获取x1,y1,x2,y2? - how to get x1,y1,x2,y2 from map coords with javascript? fengyuanchen Cropper:如何获取裁剪后图像的x1,y1,x2,y2坐标 - fengyuanchen Cropper: How to get x1, y1, x2, y2 coordinates of the cropped image 如何显示裁剪后的图像的(x1,y1,)和(x2,y2) - How to display (x1,y1,) and (x2,y2) of croped image 如何在移动 d 距离后计算具有坐标 (x1,y1),(x2,y2) 的直线的新点 (x3,y3),(x4,y4)? - How to calculate new points(x3,y3),(x4,y4) of the line having co-ordinates (x1,y1),(x2,y2) after shifting d distance?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM