jQuery getting the value of the second through tenth td of the clicked tr in a table. I already have the first

﹥>﹥吖頭↗ 提交于 2019-12-11 14:03:20

问题


                    $("#existcustomers tr").click(function () {
                        var td1 = $(this).children("td").first().text();
                        alert(td1);
                    });

I need the value of td2-td10 as well. I can't seem to figure out how to accomplish this. I tried using .second() in the same fashion but that seems to be breaking the programming. Does anyone know how this would be accomplished for the following td's?


回答1:


To get a specific cell by index, you can use :

$(this).children(":eq(1)")

To get the first 10 children, use :

$(this).children(":lt(10)")

If you want to get the content in separate cells of an array, you can do

var texts = $(this).children(":lt(10)").map(function(){return $(this).text()});

This makes an array like this :

["contentofcell1", "cell2", "3", "cell 4", "five", "six", "sieben", "otto", "neuf", "X"]



回答2:


Use eq(index) to find it easly.

$("#existcustomers tr").click(function () {
    var td1 = $(this).children("td").first().text();
    var td2 = $(this).find("td").eq(2).text();
    var td10 = $(this).find("td").eq(10).text();
    alert(td1 + "-" + td2 + "-" + td10);
});

to get values of td2 - td10 range:

$("#existcustomers tr").click(function () {
    var td1 = $(this).children("td").first().text();
    var result = "";
    for(var i=2; i<=10; i++) {
        result = result + " - " + $(this).find("td").eq(i).text();
    }
    alert(td1 + result);
});



回答3:


$(this).children("td").each(function() {
  alert($(this).text());
}

Will loop through all the tds.




回答4:


Try this

$("#existcustomers tr").click(function() {
    var td1 = "";
    // To get values of td's between 2 and 10 we should search for
    // the td's greater than 1 and less than 11...
    $.each($(this).children("td:lt(11):gt(1)"),function() {
        td1 += $(this).text();
    });
    alert(td1);
});


来源:https://stackoverflow.com/questions/13759702/jquery-getting-the-value-of-the-second-through-tenth-td-of-the-clicked-tr-in-a-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!