Uncaught TypeError: Cannot call method 'toLowerCase' of undefined

后端 未结 4 2002
挽巷
挽巷 2021-02-06 23:49

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

The line that triggered the error is the following:

$(this).closest(\'tr\')

相关标签:
4条回答
  • 2021-02-07 00:31

    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 {
          // ...
        }
      }
    );
    
    0 讨论(0)
  • 2021-02-07 00:37
    // assign this to a variable
    
    var val = this;
    
    // Use
    
    $(val).closest('tr').remove(); // It worked for me
    

    Instead of :

    $(this).closest('tr').remove();
    
    0 讨论(0)
  • 2021-02-07 00:45

    I got exactly the same error on $.get() request.

    For me this is the most weird bug in jQuery (my version 1.8.3).

    My reason for this error was TD element with ID set to "nodeName".

    After change ID value to something else, the error dissapear.

    Original:

    <td id="nodeName"></td>
    

    New:

    <td id="nodeNameValue"></td>
    
    0 讨论(0)
  • 2021-02-07 00:45

    Inside your request, $(this) is not the .remove element. It's a closure problem.

    So you can assign $(this) to a variable before and call it after

    0 讨论(0)
提交回复
热议问题