Javascript 未捕获的类型错误:无法调用未定义的方法“toLowerCase”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7334481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Uncaught TypeError: Cannot call method 'toLowerCase' of undefined
提问by cabaret
I ran into a very vague error while doing some simple jQuery DOM manipulation.
我在做一些简单的 jQuery DOM 操作时遇到了一个非常模糊的错误。
The line that triggered the error is the following:
触发错误的行如下:
$(this).closest('tr').remove();
$(this).closest('tr').remove();
The error, as stated in the title, is:
如标题所述,错误是:
Uncaught TypeError: Cannot call method 'toLowerCase' of undefined
未捕获的类型错误:无法调用未定义的方法“toLowerCase”
in file: jquery-1.6.2.js
.
在文件中:jquery-1.6.2.js
。
I have already tried with jQuery 1.6.3 and jQuery 1.5.1, but nothing changed. I still keep getting the error.
我已经尝试过 jQuery 1.6.3 和 jQuery 1.5.1,但没有任何改变。我仍然不断收到错误。
The line above is bound to a button which, when clicked, submits an AJAX POST request, then when successful, it removes its own <tr>
. I have already tried another way by doing:
上面的行绑定到一个按钮,当点击该按钮时,提交一个 AJAX POST 请求,然后当成功时,它删除自己的<tr>
. 我已经尝试过另一种方式:
$(this).parent().parent().remove()
but to my surprise, this did nothing.
$(this).parent().parent().remove()
但令我惊讶的是,这没有任何作用。
I'm using Chrome, in case it matters, but I get the same error in FF.
我正在使用 Chrome,以防万一,但我在 FF 中遇到相同的错误。
EDIT:
编辑:
This is my code:
这是我的代码:
$(".remove").live('click', function(e) {
var str = $(this).attr('id').match(/\[(.*?)\]\[(.*?)\]/);
var type = str[1];
var id = str[2];
// IF I PUT THE remove() HERE, IT WORKS.
$.ajax({
type: 'POST',
url: '/admin/ajax_remove_event_detail',
data: {type: type, id: id},
success:function(res) {
if(res)
{
$(this).closest('tr').remove();
// this gives me an error
}
else
{
// error handling
}
}
});
e.preventDefault();
});
If I put the remove() outside of the if somewhere, it works. Why won't it work when it's inside the ajax call/if? Makes no sense to me? :(
如果我将 remove() 放在 if 之外的某个地方,它会起作用。当它在 ajax 调用/if 中时,为什么它不起作用?对我没有意义?:(
Any help here? Thanks in advance.
这里有什么帮助吗?提前致谢。
回答by Pointy
I don't know what you expectthe value of this
to be in your "success" handler, but I bet it's not what you think. Save this
outside the "$.ajax()" call:
我不知道您期望的this
“成功”处理程序的价值是什么,但我敢打赌这不是您的想法。this
在 "$.ajax()" 调用之外保存:
var clicked = this;
$.ajax(
// ...
success: function(res) {
if (res) {
$(clicked).closest('tr').remove();
}
else {
// ...
}
}
);
回答by Py.
Inside your request, $(this) is not the .remove
element. It's a closure problem.
在您的请求中, $(this) 不是.remove
元素。这是一个封闭问题。
So you can assign $(this) to a variable before and call it after
因此,您可以将 $(this) 分配给变量之前并在之后调用它
回答by Nadeem Shinwari
// assign this to a variable
var val = this;
// Use
$(val).closest('tr').remove(); // It worked for me
Instead of :
代替 :
$(this).closest('tr').remove();
回答by Tomino
I got exactly the same error on $.get() request.
我在$.get() request上遇到了完全相同的错误。
For me this is the most weird bug in jQuery (my version 1.8.3).
对我来说,这是 jQuery 中最奇怪的错误(我的版本 1.8.3)。
My reason for this error was TD element with ID set to "nodeName".
我出现此错误的原因是ID 设置为 "nodeName" 的 TD 元素。
After change ID value to something else, the error dissapear.
将 ID 值更改为其他值后,错误消失。
Original:
原来的:
<td id="nodeName"></td>
New:
新的:
<td id="nodeNameValue"></td>