Javascript 使用jquery在特定行的最后一列、最后一个跨距之后添加链接

Javascript 使用jquery在特定行的最后一列、最后一个跨距之后添加链接,javascript,jquery,asp.net-mvc,Javascript,Jquery,Asp.net Mvc,我在表格的每一行中都有一个用于更改状态的下拉列表。此下拉列表将通过Ajax调用更改当前行的状态。如果新状态未完成或取消,我们应该在当前行的最后一列中看到编辑链接,在其他链接之后 $(".state-select-container").change(function () { var id = $(this).attr("data-id"); var state = $(this).val(); var url = "/

我在表格的每一行中都有一个用于更改状态的下拉列表。此下拉列表将通过Ajax调用更改当前行的状态。如果新状态未完成或取消,我们应该在当前行的最后一列中看到编辑链接,在其他链接之后

  $(".state-select-container").change(function () {
            var id = $(this).attr("data-id");
            var state = $(this).val();
            var url = "/MyController/ChangeState?requestId=" + id + "&state=" + state;
            var that = $(this);

            $.post(url, function (data) {
                if (data.updated) {
                  //some other codes
                    if (state !== 'Cancel' && state !== 'Done') { //Edit is authorized
                        var href = '/MyController/Edit?requestId='+ id +'"';

  //WHAT IS THE CORRECT CODE HERE? THE CODE, BELOW,
  //ADDS THE LINK AFTER THE CURRENT ROW, NOT IN LAST COLUMN, AFTER LAST SPAN

                        that.closest("tr").last('td').last('span')
                            .after('<a href="'+href+'">Edit</a>');
                     }

                } else {
                    alert("Error!");
                }
             });
        });
that.closest("tr").children('td:last').children('span:last').after('<a href="'+href+'">Edit</a>');

您的js代码看起来不错,但您的HTML代码是ba,也许您关闭了第一个锚标记:

<a data-id="@item.Id" href='its href' data-toggle="tooltip"></a>
that.closest("tr").children('td:last').children('span:last').after('<a href="'+href+'">Edit</a>');

编辑:

that.closest("tr").children('td:last').children('span:last').after('<a href="'+href+'">Edit</a>');
好的,html代码还可以,但是last()jquery函数不接受参数()。所以JYoThI的注释是正确的,或者使用最后一个函数: $(this).closest(“tr”).find('td').last().find('span').last()

像这样使用

that.closest("tr").find('td:last-child').find('span:last-chi‌​ld').after('<a href="'+href+'">Edit</a>');
that.closest("tr").children('td:last').children('span:last').after('<a href="'+href+'">Edit</a>');
that.closest(“tr”).find('td:last child').find('span:last chi‌​ld')。在(“”)之后;

that.closest("tr").children('td:last').children('span:last').after('<a href="'+href+'">Edit</a>');
注意:last()此方法不接受任何参数

that.closest("tr").children('td:last').children('span:last').after('<a href="'+href+'">Edit</a>');
试试下面的代码

    that.closest("tr").last().append('<a href="'+href+'">Edit</a>');
that.closest("tr").children('td:last').children('span:last').after('<a href="'+href+'">Edit</a>');
that.closest(“tr”).last().append(“”);

试试这段代码

that.closest("tr").children('td:last').children('span:last').after('<a href="'+href+'">Edit</a>');
that.closest(“tr”).children('td:last')。children('span:last')。after('');

that.closest(“tr”).find('td:last child').find('span:last child')).after('');是的。你能在下面检查一下吗@elnazThank,使用的代码有什么问题吗?我们不能使用.last()。last()?