未捕获的TypeError:无法调用未定义的方法'toLowerCase' [英] Uncaught TypeError: Cannot call method 'toLowerCase' of undefined

查看:110
本文介绍了未捕获的TypeError:无法调用未定义的方法'toLowerCase'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一些简单的jQuery DOM操作时遇到了一个非常模糊的错误。

I ran into a very vague error while doing some simple jQuery DOM manipulation.

触发错误的行如下:

$(this).closest('tr')。remove();

如标题所述,错误是:


未捕获的TypeError:无法调用未定义的方法'toLowerCase'

Uncaught TypeError: Cannot call method 'toLowerCase' of undefined

在文件中: jquery-1.6.2.js

我已经尝试过jQuery 1.6.3和jQuery 1.5.1,但没有改变。我仍然一直收到错误。

I have already tried with jQuery 1.6.3 and jQuery 1.5.1, but nothing changed. I still keep getting the error.

上面的行被绑定到一个按钮,当点击它时,提交一个AJAX POST请求,然后成功时,它会删除它自己的< TR> 。我已经尝试了另一种方法:

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:

$(this).parent()。parent()。remove()但令我惊讶的是,这没有做任何事。

$(this).parent().parent().remove() but to my surprise, this did nothing.

我正在使用Chrome,以防万一,但我在FF中遇到同样的错误。

I'm using Chrome, in case it matters, but I get the same error in FF.

编辑:

这是我的代码:

$(".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();
});

如果我将remove()放在if之外的地方,它就可以了。当它在ajax调用/ if中时为什么不能工作?对我没用? :(

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? :(

这里有任何帮助吗?先谢谢。

Any help here? Thanks in advance.

推荐答案

我不知道你期望 这个的价值在你的成功处理程序中,但我敢打赌,这不是你的想法。在$ .ajax()调用之外保存

I don't know what you expect the value of this to be in your "success" handler, but I bet it's not what you think. Save this outside the "$.ajax()" call:

var clicked = this;
$.ajax(
  // ...
  success: function(res) {
    if (res) {
      $(clicked).closest('tr').remove();
    }
    else {
      // ...
    }
  }
);

这篇关于未捕获的TypeError:无法调用未定义的方法'toLowerCase'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆