【问题标题】:Getting Uncaught TypeError: Cannot read property 'toLowerCase' of undefined [closed]获取未捕获的类型错误:无法读取未定义的属性“toLowerCase”[关闭]
【发布时间】:2020-11-17 19:46:01
【问题描述】:

在我问之前,我已经在 stackoverflow 和其他网站上搜索过这个问题,其中大多数都提到你的“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 函数是(在类中定义的)

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)

【问题讨论】:

  • 最后一个是语法错误。但除此之外,您是否尝试过$(this).val()minimal reproducible example 会很有帮助。问题只出在 JavaScript / FrontEnd,PHP 代码似乎没问题。
  • 实际问题是什么?抱歉,您的问题对于您要实现的目标以及您在哪里使用 toLowerCase 没有任何意义
  • @AlwaysHelping 这就是问题所在,我的代码中没有使用任何 toLowerCase 函数。问题是,我正在尝试验证用户名是否已被使用或未使用 AJAX,并且每当我在尝试测试代码时从“用户名”字段“失去焦点”时都会出现此错误。
  • @SakibKhan 你能做一个minimal reproducible example吗,因为这在我看来是不可复制的。
  • @PraveenKumarPurushothaman 我想我已经提供了足够的代码?我完全可以在这里提供完整的代码,但这会很长而且没有必要。我认为我的问题在于一些小语法(很可能与“this”有关),这就是我试图寻求帮助的原因.... 是的,$(this).val() 应该可以工作,但它不是(我在上面共享的代码中使用了 $(this).val())

标签: javascript jquery forms validation blur


【解决方案1】:

您没有正确使用传递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);
    }
  });
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-06
    • 2016-03-25
    • 2018-05-16
    • 2021-10-13
    • 2017-06-26
    • 1970-01-01
    • 2021-09-24
    相关资源
    最近更新 更多