javascript函数失败,错误为关于未定义变量的ReferenceError
我有以下代码:javascript函数失败,错误为关于未定义变量的ReferenceError,javascript,jquery,Javascript,Jquery,我有以下代码: //select all siblings with same value in third column thisTR.siblings().filter(function() { if ($('td',this).eq(2).text() == $('td', thisTR).eq(2).text() ) { var tds = $(this).find('td'); con
//select all siblings with same value in third column
thisTR.siblings().filter(function() {
if ($('td',this).eq(2).text() == $('td', thisTR).eq(2).text() ) {
var tds = $(this).find('td');
console.log($(this));
var tr =$("<tr/>");
tr.append("<td>" + tds.eq(0).text() + "</td>");
tr.append("<td>" + tds.eq(1).text() + "</td>");
tr.append("<td>" + tds.eq(2).text() + "</td>");
tr.append("<td>" + tds.eq(3).text() + "</td>");
tr.attr('id', id.substring(4));
$('#selected_users').append(tr);
};
});
问题
当我调用函数时,会收到一条错误消息,上面说:
ReferenceError:未定义id
在我执行此操作的函数中,代码行出现故障:
tr.attr('id', id.substring(4));
当我将代码嵌入if语句而不是函数的一部分时,代码不会失败
我到目前为止所做的尝试
我已尝试通过以下方式更改调用该方法的方式:
add_to_selected_users($(this));
为此:
add_to_selected_users(this);
但这没什么区别。对我可能遗漏的内容有什么建议吗 如果您试图使用行的ID:
tr.attr('id', row.attr('id').substring(4));
没有定义id变量
tr.attr('id', id.substring(4));
此行使用了一个名为id的变量,并试图调用名为substring的函数
id.substring(4)
代码中没有定义“id”变量。您没有按照错误说明定义
id
。这意味着,在父作用域中缺少id。你可以避免说的错误
if(typeof id == 'undefined' || id == null) id = ...
这将防止出现错误,您将再次定义
id
,因为这是需要执行的操作。tr.attr('id',id.substring(4))代码>是的,你没事。我缺乏复制/粘贴技能。我漏了一行,我的错。我忘了复制“var id=this.id;”谢谢。
id.substring(4)
if(typeof id == 'undefined' || id == null) id = ...