简体   繁体   English

JQuery - 循环遍历表行

[英]JQuery - Looping through table rows

I'm trying to loop through each tr in my table and get the value of each td where td is a certain class. 我正在尝试遍历表中的每个tr并获取每个td的值,其中td是某个类。

I'm populating my table like this: 我像这样填充我的桌子:

    var attr = "checked";

    $('#mytbody').prepend('<tr><td class="cb" value="' + attr + '"><input type="checkbox" ' + attr + ' disabled class="filterPaid" id="invPaid' + i + '" value="outstanding" name="filterTasks[]"></td>.......</tr>');

I'm trying to search like this: 我试图像这样搜索:

$("#paidFilter").change(function() {
    if(this.checked) {
        $('#mytbody  > tr > td.cb').each(function(){
            console.log($(this).val());
        });   
    }
});

However this doesn't return any values for the td of class = 'cb'. 但是,这不会返回class ='cb'的td的任何值。

How could these values be retrieved? 如何检索这些值?

something like this? 这样的事情? http://jsfiddle.net/swm53ran/129/ http://jsfiddle.net/swm53ran/129/

.val() of a td element doesnt exist like it does for an input. td元素的.val()不像输入那样存在。 for the td, value is an attribute, so you access it by .attr('value') 对于td,value是一个属性,因此您可以通过.attr('value')访问它

$(document).ready(function() {
    $('#mytbody  > tr > td.cb').each(function(){
        console.log($(this).attr('value'));
        $(this).append($(this).attr('value'));
    });   
});

<table>
    <tbody id="mytbody">
        <tr>
            <td class="cb" value="checked">CB should append value here:</td>
            <td class="cb" value="checked">CB should append value here:</td>
            <td class="" value="checked">NOT CB</td>
        </tr>
        <tr>
            <td class="cb" value="checked">CB should append value here:</td>
            <td class="" value="checked">NOT CB</td>
            <td class="cb" value="checked">CB should append value here:</td>
        </tr>
    </tbody>
</table>

you were close :] hope this helps 你很亲密:]希望这会有所帮助

$().val() is strictly intended to get value="" attributes from form inputs, such as <input> or <textarea> . $().val()严格用于从表单输入中获取value=""属性,例如<input><textarea>

If you still wish to assign a value="" attribute to the <td> element (not recommended), you can retrieve it using $('#mytbody').attr('value') . 如果您仍希望为<td>元素指定value=""属性(不推荐),则可以使用$('#mytbody').attr('value')来检索它。 The same can also be used to retrieve the value of any other attribute. 同样也可用于检索任何其他属性的值。

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

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