JQUERY—获取具有特定类的表单元格中的数据
我有如下表格:JQUERY—获取具有特定类的表单元格中的数据,jquery,javascript,Jquery,Javascript,我有如下表格: <table id="Grid"> <thead> <tr> <th>Month</th> <th>Savings</th> </tr> </thead> <tbody> <tr> <td>January</td> <td class="k-dirty-cell"
<table id="Grid">
<thead>
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
</thead>
<tbody>
<tr>
<td>January</td>
<td class="k-dirty-cell">$100</td>
</tr>
<tr>
<td>February</td>
<td class="k-dirty-cell">$80</td>
</tr>
<tr>
<td>March</td>
<td>$98</td>
</tr>
</tbody>
</table>
如果可以的话,请帮忙好吗
非常感谢 如果您的主要目标是
,我希望使用class=“k-dirty-cell”遍历(或解析)单元格,并获取其值并验证其是否为null。
,以下内容就足够了:
$(function() {
$('td.k-dirty-cell').each(function() {
var cellValue = $(this).text();
//do something with cellValue
if(cellValue) {
//not null
} else {
//null
}
});
});
编辑:
由于可能存在类位于第一列的情况(根据注释讨论),以下内容将仅选择第二列的k-dirty-cell
类项目:
$(function() {
$('tr td:nth-child(2).k-dirty-cell').each(function() {
var cellValue = $(this).text();
alert(cellValue)
//do something with cellValue
if(cellValue) {
//not null
} else {
//null
}
});
});
小提琴示例:
你只需要获得所有类别为
k-dirty-cell
的td
标签,这样就足够了:
$("#Grid td.k-dirty-cell").each(function() {
if($(this).text().length > 0) {
alert("value: " + $(this).text());
}
}
td.k-dirty-cell,不是-cell@kennypu是的,但我只希望它在cellIndex为2的位置执行此检查,即一行中的第二列。我如何更改上面的内容以反映这一点?@t_plusplus那么当类位于第一列时是否存在这种情况?如果没有,就没有什么可以改变的。根据您的帖子,该类始终位于第二列,因此无需更改任何内容,因为它将自动选择第二列only@kennypu是的,事实上确实存在这样的情况,但我只想验证第二列,而不是第一列。因此,即使k-dirty-cell存在于其他列中,找到它也不重要。谢谢。@t_plusplus请参见修改后的答案如果您想确保它们只是您正在获取的td元素,您可以指定td.k-dirty-cell。
$(".k-dirty-cell").each(function() {
// your code
});
$("#Grid td.k-dirty-cell").each(function() {
if($(this).text().length > 0) {
alert("value: " + $(this).text());
}
}