在 jQuery 中选择特定的 td
如果行包含特定值,我想检查表的复选框。
下面是我的表
<table width="100%" cellspacing="0" border="1" style="border-width: thin; border-collapse: collapse;
border-color: #999999" id="tableControlFailures">
<tbody>
<tr>
<td style="border-width: thin; border-collapse: collapse; border-color: #999999">
<input type="checkbox" id="chkCF">
</td>
<td style="border-width: thin; border-collapse: collapse; border-color: #999999">
C1
</td>
<td style="border-width: thin; border-collapse: collapse; border-color: #999999"
id="tdCFReason">
<input type="text" id="txtCFReason" style="width: 90%" disabled="disabled">
</td>
<td id="CFId" style="display: none">
22
</td>
</tr>
<tr>
<td style="border-width: thin; border-collapse: collapse; border-color: #999999">
<input type="checkbox" id="chkCF">
</td>
<td style="border-width: thin; border-collapse: collapse; border-color: #999999">
C2
</td>
<td style="border-width: thin; border-collapse: collapse; border-color: #999999"
id="tdCFReason">
<input type="text" id="txtCFReason" style="width: 90%" disabled="disabled">
</td>
<td id="CFId" style="display: none">
23
</td>
</tr>
<tr>
<td style="border-width: thin; border-collapse: collapse; border-color: #999999">
<input type="checkbox" id="chkCF">
</td>
<td style="border-width: thin; border-collapse: collapse; border-color: #999999">
c
</td>
<td style="border-width: thin; border-collapse: collapse; border-color: #999999"
id="tdCFReason">
<input type="text" id="txtCFReason" style="width: 90%" disabled="disabled">
</td>
<td id="CFId" style="display: none">
9
</td>
</tr>
</tbody>
</table>
,我想检查隐藏列 CFId 包含 23 & 的行。 9.
请提出解决方案。
I want to check checkboxes of the table if the rows contain specific value.
Below is my table
<table width="100%" cellspacing="0" border="1" style="border-width: thin; border-collapse: collapse;
border-color: #999999" id="tableControlFailures">
<tbody>
<tr>
<td style="border-width: thin; border-collapse: collapse; border-color: #999999">
<input type="checkbox" id="chkCF">
</td>
<td style="border-width: thin; border-collapse: collapse; border-color: #999999">
C1
</td>
<td style="border-width: thin; border-collapse: collapse; border-color: #999999"
id="tdCFReason">
<input type="text" id="txtCFReason" style="width: 90%" disabled="disabled">
</td>
<td id="CFId" style="display: none">
22
</td>
</tr>
<tr>
<td style="border-width: thin; border-collapse: collapse; border-color: #999999">
<input type="checkbox" id="chkCF">
</td>
<td style="border-width: thin; border-collapse: collapse; border-color: #999999">
C2
</td>
<td style="border-width: thin; border-collapse: collapse; border-color: #999999"
id="tdCFReason">
<input type="text" id="txtCFReason" style="width: 90%" disabled="disabled">
</td>
<td id="CFId" style="display: none">
23
</td>
</tr>
<tr>
<td style="border-width: thin; border-collapse: collapse; border-color: #999999">
<input type="checkbox" id="chkCF">
</td>
<td style="border-width: thin; border-collapse: collapse; border-color: #999999">
c
</td>
<td style="border-width: thin; border-collapse: collapse; border-color: #999999"
id="tdCFReason">
<input type="text" id="txtCFReason" style="width: 90%" disabled="disabled">
</td>
<td id="CFId" style="display: none">
9
</td>
</tr>
</tbody>
</table>
I want to check the rows where hidden column CFId contains 23 & 9.
Please suggestion a solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
一件非常重要的事情,正如其他人也提到的:
ID 只能在 HTML 文档中使用一次。在您的标记中,您多次使用 ID,这是无效的,并且会破坏 CSS和 JavaScript 功能。如果要将某些元素分配到同一逻辑组中,请改用
class
。我的解决方案在将这些 ID 更改为类后起作用(如 jsFiddle 中所示)。jsFiddle 演示
td.CFId
里面包含 23 或 9注意: 我的解决方案是为 jQuery 1.5.2 编写的。在 1.6 中,您应该使用
.prop('checked', true)
而不是.attr('checked', true)
。A very important thing, as others also mentioned:
An ID can only be used ONCE in a HTML document. In your markup you're using IDs multiple times, which is simply not valid and will break both CSS and Javascript functionalities. If you want to assign some elements into the same logical group, use
class
instead. My solution works after changing those IDs into classes (as demonstrated in the jsFiddle).jsFiddle Demo
td.CFId
inside that contains either 23 or 9Note: My solution is written for jQuery 1.5.2. In 1.6, you should use
.prop('checked', true)
instead of.attr('checked', true)
.我建议你稍微修改一下标记。您正在重复表格单元格的 id id="CFId",而不是尝试 class="CFId"。 JQuery 提供了使用 CSS 规则读取此类元素的解决方案。因此,您应该能够使用“td.CFId”CSS 规则读取具有 CFId 类的表格单元格。
然后迭代它并检查值。要到达该行,您可以尝试 ele.parentNode 进行 CSS 修改,以根据值更改颜色和背景。
除非您只是不想坚持使用 JQuery,否则该解决方案应该可以使用 JQuery API 和简单的 JavaScript 为您工作。
I would suggest you to rework little on the mark-up. You are repeating the id id="CFId" for table cell instead try class="CFId". JQuery offers solutions to read such elements using the CSS rules. So you should be able to read table cells with class CFId class using "td.CFId" css rule.
Then iterate it and check values. to reach to the row you can try ele.parentNode to do the CSS modifications to change the color and background based on the values.
Unless you just don't want to stick the JQuery this solution should work for you with JQuery API and simple JavaScript.
尝试这个
示例 JS FIDDLE
但是,正如其他人所说,您应该更改您的 td 以具有唯一的 id并使用类对元素进行分组。在您设置唯一 ID 后,我的解决方案仍然有效
Try this
Example at JS FIDDLE
However, as others have said, you should change your td to have unique ids and use the class to group elements. My solution will still work after you set unique ids