jQuery迭代输出的Ci / Sql数据

时间:2012-04-06 04:33:54

标签: javascript jquery codeigniter

我有一个Ci脚本输出数据库值。在这些值上,#CimentLink'和'#commentBox'在Ci输出数据的迭代中输出。我如何识别哪个commentLink和commentBox是哪个。现在,如果您单击第一个commentLink,它会选择第一个commentBox。当你点击第20个commentLink时,它会选择第一个commentBox。我需要第20个commentLink来选择第20个commentBox。

那么我如何迭代这些值呢?有没有办法使用唯一的ID识别输出的行并使用.each()或某种性质?

$("#commentLink").click(function() {
        $("#commentBox").focus();
});

1 个答案:

答案 0 :(得分:1)

而不是通过id使用选择器,您可以使用遍历来获取您感兴趣的节点。例如,如果每个都在表格行中:

<tr>
    <td><input id="commentBox" type="text>SomeText</input></td>
    <td><input id="anotherBox" type="text>SomeText</input></td>
    <td><a id="commentLink">Click me!</a></td>
</tr>

你可以在函数体中说$(this).parent.sibling('#commentBox')来获得同一行中的一个#commentbox。

请参阅jQuery click documentation的示例部分,了解$(this)的使用方式。

此外,作为次要问题,在页面上保持ID唯一并使用类似于此类的内容(commentBox)是一种良好的做法。