使用JQuery,如何将click事件发送到动态表的一行?

时间:2012-08-02 14:27:51

标签: jquery javascript-events

我有一个ajax表单,它返回一个列表并将其放入HTML表中。我计划在ajax调用的Complete部分中使用click事件,但我不确定如何处理。

我需要的是一种能够: a)确定表中是否有任何行,并且 b)向该行发送点击事件。

我知道如何执行click事件,它是导致我暂停的动态表。 感谢帮助, 车

1 个答案:

答案 0 :(得分:3)

没有测试它,但这应该做的工作:

alert( $('#myTable tr').length )

放置表格的任何ID,类或其他内容,然后选择所有子元素tr。使用函数长度,她将返回表中tr的数量。如您所知,每个tr都是一行。

所以在最后,你的代码将如下所示:

if( $('#myTable tr').length > 0 ){
    // You have more then 1 row !
    $('#yourBtn').trigger('click');
}

如果您想点击第一行,无论您做什么:

$('#myTable tr:first-child').trigger('click');

但是,单击一行不是最好的主意,因为行不是链接。如果您想点击TD中的链接,可以尝试:

// Will find first TR (row)
// Will go to the TD with the index "1". I think it's the second TD because index start     at 0, but I am not sure anymore.
// Will find the first link, then trigger it as a "click".
$('#myTable tr:first-child').find('td:eq(1) a').trigger('click');

没试过最后一个,所以我不知道它是否有用。