如何使用jquery检查表行上是否存在id属性

时间:2015-02-26 17:14:40

标签: jquery

我在ajax调用后为表行分配id属性。在另一个函数调用我需要检查此行是否已分配id并尝试以下代码,但在此行脚本上不断收到错误。 Uncaught TypeError: undefined is not a function

if (typeof($(this).closest('tr').Attr('id'))!='undefined') {
        rowId = $(this).closest('tr').attr('id');
    }

我已经看过类似的问题和解决方案,尝试了各种组合,但不知道为什么我一直在这条线上出错。

2 个答案:

答案 0 :(得分:1)

方法名称为attr() - 请注意小写a。您也不需要使用typeof。空字符串或null在比较中使用时返回false,因此您只需在if条件下使用方法调用:

if ($(this).closest('tr').attr('id')) {
    rowId = $(this).closest('tr').attr('id');
    // do something with rowId...
}

答案 1 :(得分:-1)

您的选择器似乎没有选择元素,因此未定义。

您需要确保$(this)实际设置正确。