当行包含选中的复选框时,我想选择td的值。我可以选择包含已选中复选框的行,但是无法在该行上进行迭代以选择键。

这是表结构。

<table class="test"><thead><tr>
<th class="table-header-name">select</th>
<th class="placeholder" style="min-width:100px"></th>
<th class="table-header-name">Key</th>
<th class="table-header-name">hash</th>
<th class="table-header-name">type</th>
</tr>
</thead>

<tbody id="table-body">
<tr class="table-row">
<td><input class="change-select" type="checkbox"></td>
<td class="placeholder"></td>
<td class="key">TICKET:44433</td>
<td class="hash">444nhh</td>
<td class="type">COOL</td>
</tr>
</tbody></table>


我要查找的值是“ key”类的td-tag中的文本。

<td class="key">TICKET:44433</td>


首先,我选择包含已选中复选框的行:

 AJS.$('.test tr :has(:checkbox:checked)').each(function (i,val) {
                console.log("this no:"+i+": " + val);
                // get td of class "key" and print the value

                })

            });


好吧,我尝试了很多方法来获得td的价值,例如:

this.find("td .key").text();
this.find("td").eq([number])


在这两种情况下,它都不会返回任何内容。

我发现使用HTMLTableCellElements时要遍历'.aui tr :has(:checkbox:checked)',但在此相关主题中应该可以使用:Jquery Selecting Table Rows with Checkbox

  console.log("find td: " + AJS.$(this).find('td'));
                AJS.$(this).find('td').each(function(key,element){
                    console.log('key: ' + key + '\n' + 'value: ' + element);
                })


将生成以下输出:

"find td: [object Object]"


而已...

所以我想我选择行的方式一定有问题。希望您能在这里给我一个提示。

最佳答案

$(document).ready(function(){
    $('tbody tr').each(function(){
        if($(this).find('.change-select').attr('checked') == 'checked'){
        alert($(this).find('.key').text());
        }
    });
});


DEMO Here

08-18 07:39
查看更多