我需要提取表中每一列的详细信息.例如,列"名称/编号".

  • 该表包含多个地址
  • The very last column of each row has a button that lets a user choose a listed address.

Problem:我的代码只选取了第一个<td>nr.我怎样才能让它工作?

Here's the jQuery bit:

$(".use-address").click(function() {
    var id = $("#choose-addreslayui-table").find(".nr:first").text();
    $("#resultas").append(id); // Testing: append the contents of the td to a div
});

Table:

<table id="choose-addreslayui-table" class="ui-widget ui-widget-content">
    <thead>
        <tr class="ui-widget-header ">
            <th>Name/Nr.</th>
            <th>Street</th>
            <th>Town</th>
            <th>Postcode</th>
            <th>Country</th>
            <th>Options</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td class="nr"><span>50</span>
            </td>
            <td>Some Street 1</td>
            <td>Leeds</td>
            <td>L0 0XX</td>
            <td>United Kingdom</td>
            <td>
                <button type="button" class="use-address" />
            </td>
        </tr>
        <tr>
            <td class="nr">49</td>
            <td>Some Street 2</td>
            <td>Lancaster</td>
            <td>L0 0XX</td>
            <td>United Kingdom</td>
            <td>
                <button type="button" class="use-address" />
            </td>
        </tr>
    </tbody>
</table>

推荐答案

The object of the exercise is to find the row that contains the information. When we get there, we can easily extract the required information.

Answer

$(".use-address").click(function() {
    var $item = $(this).closest("tr")   // Finds the closest row <tr> 
                       .find(".nr")     // Gets a descendent with class="nr"
                       .text();         // Retrieves the text within <td>

    $("#resultas").append($item);       // Outputs the answer
});

VIEW DEMO

Now let's focus on some frequently asked questions in such situations.

How to find the closest row?

Using 100:

var $row = $(this).closest("tr");

Using .parent():

You can also move up the DOM tree using .parent() method. This is just an alternative that is sometimes used together with .prev() and .next().

var $row = $(this).parent()             // Moves up from <button> to <td>
                  .parent();            // Moves up from <td> to <tr>

Getting all table cell <td> values

因此,我们有$row个单元格,我们想要输出表格单元格文本:

var $row = $(this).closest("tr"),       // Finds the closest row <tr> 
    $tds = $row.find("td");             // Finds all children <td> elements

$.each($tds, function() {               // Visits every single <td> element
    console.log($(this).text());        // Prints out the text within the <td>
});

100

Getting a specific <td> value

Similar to the previous one, however we can specify the index of the child <td> element.

var $row = $(this).closest("tr"),        // Finds the closest row <tr> 
    $tds = $row.find("td:nth-child(2)"); // Finds the 2nd <td> element

$.each($tds, function() {                // Visits every single <td> element
    console.log($(this).text());         // Prints out the text within the <td>
});

VIEW DEMO

Useful methods

  • .closest()-获取与 Select 符匹配的第一个元素
  • .parent()-获取当前匹配元素集中每个元素的父级
  • .parents()-获取当前匹配元素集中每个元素的祖先
  • .children()-获取匹配元素集中每个元素的子元素
  • .siblings()-获取匹配元素集中每个元素的同级
  • .find() - get the descendants of each element in the current set of matched elements
  • .next() - get the immediately following sibling of each element in the set of matched elements
  • .prev()-获取匹配元素集中每个元素的前一个同级

Jquery相关问答推荐

在添加_renderMenu方法时,是什么原因导致jQuery UI AutoComplete抛出TypeError?

jQuery从JSON创建嵌套列表

ajax调用后Jquery事件不会触发

为什么我的 toFixed() 函数不起作用?

如何用 javascript/jquery 替换 url 参数?

[本机代码]是什么意思?

这些 jQuery 就绪函数之间有什么区别?

找到下一个匹配的sibling 姐妹的有效,简洁的方法?

如何使div全屏?

如何使用 jquery 更改 onclick 事件?

如何通过 DOM 容器访问 Highcharts 图表?

JQuery:如果 div 可见

如果不是 jQuery,Javascript 中的美元符号是什么

jquery可排序占位符高度问题

jquery更改div类的样式属性

更改 div 的内容 - jQuery

只绑定一次事件

.success() 和 .complete() 之间的区别?

JQuery 判断 DOM 中的重复 id

在 JavaScript 开头使用分号的目的是什么?