如何使用jQuery在html表中获取当前行中第一列的值 [英] How to get value of first column in current row in html table using jQuery

查看:386
本文介绍了如何使用jQuery在html表中获取当前行中第一列的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下删除使用jQuery的确认代码。问题是它假定名称列的位置。我现在在第一行有名字。而不是使用 .prev('td'),有没有办法使用jQuery从当前行的第一列获取值?

I have the following Delete confirmation code using jquery. The issue is that it assume the location of the name column. I now have name in the first row. Instead of using .prev('td'), is there anyway to get the value from the first column in the current row using jQuery?

<script type='text/javascript'>
    $(document).ready(function() {
        $("a.delete").click(function(e) {
            e.preventDefault();
            var url = $(this).attr("href");

            var name = $(this).parent().prev('td').prev('td').text();

            jConfirm('Are you sure you want to delete this:' + name, 'Application Delete', function(r) {
                if (r) {
                    window.location = url;
                }
            });
        });
    });
</script>


推荐答案

您可以在每个

You could use under each

var name = $(this).parents('tr:first').find('td:first').text();

这行到行然后到行的第一个单元格。这更直观。

this goes to the row and then to the first cell of the row. That's more intuitive.

编辑:我将'tr'更改为'tr:first',但实际上通常不需要这么做,因为嵌套表往往表明整个设计(布局表)需要的不同问题以不同的方式修正。

Edit: I changed 'tr' to 'tr:first', but in real this should often not be needed, because nested tables more than often indicate a different problem in the whole design (tables for layout?) which needs to be fixed differently.

这篇关于如何使用jQuery在html表中获取当前行中第一列的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆