Getting Uncaught TypeError: Cannot read property 'toLowerCase' of undefined

Getting Uncaught TypeError: Cannot read property 'toLowerCase' of undefined

在我问之前,我也在 Whosebug 和其他网站上搜索了这个问题,他们中的大多数都提到你的“this”没有指向你试图从中获取价值的东西。

那是因为它们中的大多数都从目标元素调用了不同的函数。但是我的情况有点不同,这并没有解决我的问题,

我的HTML代码是一个表单,其中一个字段是用户名并且有这个代码,

<label for="username">Username</label>
                     <input type="text" id = "t_username" name="t_username" placeholder="Enter Teacher Username" value="" class="form-control" >
                     </div>
                     <span id = "availability"></span>

我的JQuery代码如下

 $('#t_username').blur(function()
      {
        var username = $(this).val(); 
        $.ajax({
          url: "action.php", 
          method: "POST", 
          data: {user_name: t_username}, 
          dataType: "text", 
          success:function(html)
          {
            $('#availability').html(html); 
          }
        });
      });

而我的PHP代码如下,

if (isset ($_POST['username']))
{
  $user = $_POST['username'];
  if ($db->username_check($user) != 0) // $db is an object of Database class 
  {
    echo '<span class = "text-danger">Username already exists</span>';
  }
  else 
  {
        echo '<span class = "text-success">Username is available</span>';
  }

}

我的 SQL 函数是(在 class 中定义)

public function username_check($user)
    {
      $sql = "SELECT * from tbl_teacher where username = :user";
      $stmt = $this->conn->prepare($sql);
      $stmt->execute(['user'=>$user]);
      $t_rows = $stmt->rowCount();
      return $t_rows;
    }

我试过使用这两个东西,但我仍然遇到同样的错误。谁能帮我解决这个问题?

我试过了(但没有什么好结果)

var username = $(this.target).val();   -> This called an error of exceeded stack limit (because of some never ending recursive function I think) 

var username = $('#t_usernmae').val(); -> This did the same exceeded stack limit error 

我也试过改变我的功能,

$(#'t_username').on("blur", function(){.....*Everything here* ..... }); 

JS Fiddle of the code (I don't know how to create it, but I have shared a bit of code here)

您没有正确使用传递 data

您需要使用 username 但您使用的是 t_username 它将是 undefined 而不是 maximum call stack size exceededtoLowerCase 将抛出错误控制台。

将您的 ajax jQuery 代码更改为此。一切都应该没问题。

$('#t_username').blur(function() {

  //Username
  var username = $(this).val();
  
  console.log(username)
  
  //Ajax
  $.ajax({
    url: "action.php",
    method: "POST",
    data: {
      user_name: username  //this needed a fix
    },
    dataType: "text",
    success: function(html) {
      $('#availability').html(html);
    }
  });
});