gpt4 book ai didi

javascript - 由于某种原因 jQuery 出现错误,我不明白为什么

转载 作者:行者123 更新时间:2023-12-02 14:27:40 25 4
gpt4 key购买 nike

有人可以看一下吗?我相信 jquery 源代码有问题,但我无法给出比这更具体的解释。

https://codepen.io/SebastianSapien/pen/wWJXMB

app.js:

//Hide hints
$("form span").css("display", "none");

//create som variables to make the code easier to follow
var $password = $("#password");
var $confirmPassword = $("#confirmPassword");

//when focusing (clicking) on the password text input, show the
//hints until the length of the input reaches 8.

//specifies whether the password specified is long enough
function passwordIEnough() {
return $(this).val().length > 8;
}

//specifies if the password and confirm password boxes are equal
function passwordsAreEqual() {
return $password.val() === $confirmPassword.val();
}


function passwordEvent() {
if ( passwordIsEnough() ) {
$(this).next().css("display", "none");
} else {
$(this).next().css("display", "inline-block");
}
}

function confirmPasswordEvent() {
if ( passwordsAreEqual() ) {
$(this).next().css("display", "none");
} else {
$(this).next().css("display", "inline-block");
}
}

html:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Dropdown</title>
<link rel="stylesheet" href="css/style.css" charset="utf-8">
</head>
<body>

<form action="#" method="post">
<h2>Professional Form</h2>
<label for="username">Username</label>
<input type="text" id="uname" name="username">

<label for="password">Password</label>
<input type="password" id="password" name="password">
<!-- <div class="arrow-left"></div> -->
<span>Atleast 8 characters</span>

<label for="confirmPassword">Confirm Password</label>
<input type="password" id="confirmPassword" name="confirmPassword">
<!-- <div class="arrow-left"></div> -->
<span>Please confirm your password</span>

<input type="submit" value="Submit" id="submit">
</form>


<script src="https://code.jquery.com/jquery-1.12.4.min.js" integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ=" crossorigin="anonymous"></script>
<script src="js/app.js" type="text/javascript" charset="utf-8"></script>
</body>
</html>



$password.focus(passwordEvent).keyup(passwordEvent).focus(confirmPasswordEvent).keyup(confirmPasswordEvent);

$confirmPassword.focus(confirmPasswordEvent).keyup(confirmPasswordEvent);

控制台显示的内容:

Uncaught TypeError: Cannot read property 'toLowerCase' of undefined jquery-1.12.4.min.js:4

最佳答案

问题是,在您的情况下,“this”是对窗口的引用,而不是您期望的文本框。 $(this) 只能在绑定(bind)的事件处理程序内部工作。尝试使用不同的方法来访问文本框,例如 $("#password").val(),或使用 $("#password").focus(function(){ }) 为你的逻辑。

关于javascript - 由于某种原因 jQuery 出现错误,我不明白为什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38088290/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com