简体   繁体   English

jQuery选择器

[英]JQuery selector

I have the following HTML Table: 我有以下HTML表:

<table class="display" style="width: 1000px;" id="failoverServers">
    <thead>
        <tr>
            <th class="row_selector_head"> <input type='checkbox' class='select_all_chkbx'/></th>
            <th><div>{% trans "IP ADDRESS" %}</div></th>
            <th><div>{% trans "VIRTUAL IP ADDRESS" %}</div></th>
            <th><div>{% trans "USERNAME" %}</div></th>
            <th><div></div></th>
        </tr>
    </thead>

    <tbody>
    {% for i in list %}
        <tr rel="{{ i.id }}">
            <td class="row_selector"><input type='checkbox'/></td>
            <td rel="ip">{{ i.ipAddress }}</td>
            <td rel="virtualIP">{{ i.virtualIpAddress }}</td>
            <td rel="username">{{ i.username }}</td>

        </tr>
    {% endfor %}

    </tbody>
</table>

If I do the following: 如果我执行以下操作:

var list = "";
            $('#failoverServers td.row_selector.selected').each(function() {
            var row = $(this).closest('tr');
            var id = row.attr('rel');
            list += id + " ";           
        });

Then I have a list of the numbers in the very first checkbox column. 然后在第一个复选框列中有一个数字列表。

I want to get a list of the following from the table: 我想从表中获取以下列表:

<td rel="ip">{{ i.ipAddress }}</td>

What jquery selector do I use for this?? 我为此使用什么jquery选择器?

Thanks 谢谢

Try this: 尝试这个:

var list = [];

$('#failoverServers td.row_selector.selected').each(function() {
    var row = $(this).closest('tr');
    list.push($(row).children("td[rel=ip]")[0].text());
});

Thanks for all the comments. 感谢所有的评论。 I have got the following working: 我有以下工作:

 var list = [];
  $('#failoverServers td.row_selector.selected').each(function() {
        var row = $(this).closest('tr');
        var serverIp = row.find('td[rel=ip]').text();
        list += serverIp + " ";         
    });

What I want to do is to just get all the ip items from the table whether they are selected or now (Just get all them) How do I do this? 我想做的就是从表中获取所有ip项目,无论它们是已选择还是现在(仅获取全部)。我该怎么做?

Here is your working example (static so that I could test). 这是您的工作示例(静态,以便我可以测试)。 Place your template placeholders accordingly and change your markup (if you want to be standarts complient): your table header contains 5 cells, but td's in the rows are just 4, divs should not be placed in td s - my IDE is screaming :) and these 'rel's - why do you use them - rels should be used in links. 相应地放置模板占位符并更改标记(如果您希望符合标准格式):表头包含5个单元格,但行中的td仅4个,div不应放置在td s中-我的IDE尖叫:)这些“ rel”(为什么要使用它们)“ rels”应在链接中使用。 I think "id" and "class" are sufficient to achieve everything you need ("id's" for unique elements, "class" for as many as you want). 我认为“ id”和“ class”足以满足您所需的一切(“ id”代表唯一的元素,“ class”代表您想要的任何数量)。

<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script src="jquery181min.js" type="text/javascript"></script>
        <script>

        $(document).ready(function(){

        var values = '';    
            $('input:checkbox:checked.row_selector').parent().siblings(".ip").each(function() {
            values += '  ' + $(this).text();    
            });

        console.log(values);
        });    

        </script>
    </head>
    <body>

        <table class="display" style="width: 1000px;" id="failoverServers" border =1>
        <thead>
            <tr>
                <th class="row_selector_head"> <input type='checkbox' name="name" value="asdf" class='select_all_chkbx' checked="checked"/></th>
                <th>IP Address</th>
                <th>Virtual Ip Address</th>
                <th>Username</th>
            </tr>
        </thead>

        <tbody>
            <tr id="id1">
                <td><input class="row_selector" type='checkbox' name="name" value="val1" checked="checked" /></td>
                <td class="ip">123.123.123.123</td>
                <td class="virtualIP">55.55.55.55</td>
                <td class="username">Jean Valzean</td>
            </tr>
            <tr id="id2">
                <td><input class="row_selector" type='checkbox' name="name" value="val2"/></td>
                <td class="ip">125.125.125.124</td>
                <td class="virtualIP">22.22.22.22</td>
                <td class="username">Peter Meter</td>
            </tr>
                    <tr id="id3">
                <td><input class="row_selector" type='checkbox' name="name" value="val3" checked="checked" /></td>
                <td class="ip">192.168.33.24</td>
                <td class="virtualIP">33.33.33.33</td>
                <td class="username">Joe Schmoe</td>
            </tr>
        </tbody>
        </table>
    </body>
</html>

And result is: 结果是: jQuery选择td数据检索结果

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM