【发布时间】:2016-07-03 19:21:12
【问题描述】:
我在使用 jQuery 选择元素时遇到了一个基本问题。我在表中动态添加了表行,每行都有一个动态生成的 ID。
当表格行没有 ID 时,我基本上使用了这段代码。现在,我添加了其他需要为表格行分配 ID 的功能。
这是我之前用来切换每行中某些列的代码:
$("#row .col[data-col='" + col + "']").toggle();
这就是我为行分配 ID 的方式:
<tr id="row_'+(currentIndex-1)+'">
我现在如何使用 jQuery 选择上面带有动态 ID 的行并将其与我的代码一起使用来切换我的列?
我试过了,但是 id 不起作用:
$("#row_'+(currentIndex-1)+' .col[data-col='" + col + "']").toggle();
【问题讨论】:
-
您遇到了什么错误?因为你提到的代码有一些与“”相关的问题。
$("#row_" + (currentIndex-1) + " .col[data-col='" + col + "']").toggle();试试这个 -
我在控制台中收到此错误:
Uncaught Error: Syntax error, unrecognized expression: #row_'+(currentIndex-1)+' .col[data-col='column1'] -
是的,您的语法有错误...试试这个
$("#row_" + (currentIndex-1) + " .col[data-col='" + col + "']").toggle(); -
那行得通!太感谢了。请添加它作为答案,以便我接受。谢谢你:)
标签: javascript jquery