慕沐林林
2019-12-09 14:14:32
我需要提取表中每列的詳細(xì)信息。例如,列“名稱(chēng)/編號(hào)”。該表包含多個(gè)地址每行的最后一列都有一個(gè)按鈕,使用戶(hù)可以選擇列出的地址。問(wèn)題:我的代碼僅拾取<td>具有類(lèi)的第一個(gè)nr。我該如何工作?這是jQuery位:$(".use-address").click(function() { var id = $("#choose-address-table").find(".nr:first").text(); $("#resultas").append(id); // Testing: append the contents of the td to a div});表:<table id="choose-address-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>
3 回答

慕標(biāo)琳琳
TA貢獻(xiàn)1830條經(jīng)驗(yàn) 獲得超9個(gè)贊
您需要更改代碼以找到與單擊的按鈕相關(guān)的行。嘗試這個(gè):
$(".use-address").click(function() {
var id = $(this).closest("tr").find(".nr").text();
$("#resultas").append(id);
});

冉冉說(shuō)
TA貢獻(xiàn)1877條經(jīng)驗(yàn) 獲得超1個(gè)贊
嘗試這個(gè):
$(".use-address").click(function() {
$(this).closest('tr').find('td').each(function() {
var textval = $(this).text(); // this will be the text of each <td>
});
});
這將找到tr當(dāng)前單擊的按鈕中最接近的(在DOM中向上移動(dòng)),然后循環(huán)每個(gè)按鈕td-您可能要使用值創(chuàng)建一個(gè)字符串/數(shù)組。
添加回答
舉報(bào)
0/150
提交
取消