【问题标题】:access tr and its content in a table with jquery使用 jquery 在表中访问 tr 及其内容
【发布时间】:2010-11-11 14:35:45
【问题描述】:

我正在使用 C# 应用程序进行 MVC。我试图访问表中的整行并使用 jquery 更改每个单元格中的值。

json 调用成功后,我需要更改每个 td 中的值。

请对此提出建议

【问题讨论】:

    标签: jquery html asp.net-mvc json


    【解决方案1】:

    假设您的表的“ID”是“myTable”。

    $.getJSON('http://yourpageurl.com',
        function(data)
        {
            //Let's say you want to modify all the cells in the first row.
            $('#myTable tr:first td')
                .each(
                    function()
                    {
                        //'this' represens the cell in the first row.
                        $(this).html('changing cell value');
                    }
                );
    
            //If you want to access all cells of all the rows, replace
            //#myTable tr:first td with
            //'#myTable td'    
    
    
        }
    );
    

    编辑:

    如果你知道你的 tr 的 'id',你可以替换

    '#myTable tr:first td' 选择器带有 '#' 将 替换为您的 TR id。

    【讨论】:

    • 我表中的 tr 将具有其 id 的动态值。在那种情况下,如何访问。 Json 调用将在一个 javascript 函数中,如下所示: function SubmitReport(id) { //json goes here }
    • $(this).html('改变单元格值');应该替换为 $(this).children('td').html('改变单元格值');否则,您将用新值替换整个 td 元素,据我所知,您只想替换 td 的内容。
    • RaYell,感谢您指出错误。我更正了选择器。
    • Prasad,如果您的 TR 具有动态 ID 并且您知道它们,则可以使用它们访问特定行中的单元格。
    猜你喜欢
    • 2021-06-02
    • 2021-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-21
    相关资源
    最近更新 更多