chu*_*ley 75 javascript jquery html-table onclick button
我需要提取表格中每列的详细信息.例如,列"Name/Nr.".
问题:我的代码只选择了第一个<td>有类的代码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
});
Run Code Online (Sandbox Code Playgroud)
表:
<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>
Run Code Online (Sandbox Code Playgroud)
mar*_*nas 228
练习的目的是找到包含信息的行.当我们到达那里时,我们可以轻松地提取所需的信息.
$(".use-address").click(function() {
var $item = $(this).closest("tr") // Finds the closest row <tr>
.find(".nr") // Gets a descendent with class="nr"
.text(); // Retrieves the text within <td>
$("#resultas").append($item); // Outputs the answer
});
Run Code Online (Sandbox Code Playgroud)
现在让我们关注这种情况下的一些常见问题.
使用.closest():
var $row = $(this).closest("tr");
Run Code Online (Sandbox Code Playgroud)
使用.parent():
您还可以使用.parent()方法向上移动DOM树.这只是有时与.prev()和一起使用的替代方案.next().
var $row = $(this).parent() // Moves up from <button> to <td>
.parent(); // Moves up from <td> to <tr>
Run Code Online (Sandbox Code Playgroud)
<td>值所以我们有我们的$row,我们想输出表格单元格文本:
var $row = $(this).closest("tr"), // Finds the closest row <tr>
$tds = $row.find("td"); // Finds all children <td> elements
$.each($tds, function() { // Visits every single <td> element
console.log($(this).text()); // Prints out the text within the <td>
});
Run Code Online (Sandbox Code Playgroud)
<td>价值与前一个类似,但我们可以指定子<td>元素的索引.
var $row = $(this).closest("tr"), // Finds the closest row <tr>
$tds = $row.find("td:nth-child(2)"); // Finds the 2nd <td> element
$.each($tds, function() { // Visits every single <td> element
console.log($(this).text()); // Prints out the text within the <td>
});
Run Code Online (Sandbox Code Playgroud)
.closest() - 获取与选择器匹配的第一个元素.parent() - 获取当前匹配元素集中每个元素的父元素.parents() - 获取当前匹配元素集中每个元素的祖先.children() - 获取匹配元素集中每个元素的子元素.siblings() - 获取匹配元素集中每个元素的兄弟姐妹.find() - 获取当前匹配元素集中每个元素的后代.next() - 获得匹配元素集中每个元素的紧随其后的兄弟.prev() - 获取匹配元素集中每个元素的前一个兄弟您需要更改代码以找到相对于单击按钮的行.试试这个:
$(".use-address").click(function() {
var id = $(this).closest("tr").find(".nr").text();
$("#resultas").append(id);
});
Run Code Online (Sandbox Code Playgroud)
试试这个:
$(".use-address").click(function() {
$(this).closest('tr').find('td').each(function() {
var textval = $(this).text(); // this will be the text of each <td>
});
});
Run Code Online (Sandbox Code Playgroud)
这将找到tr当前单击按钮的最近(通过DOM),然后循环每个td- 您可能想要创建一个包含值的字符串/数组.