我创建了一个包含键盘事件的jQuery外部文件confirmPassword.js
但是当我尝试在我的html文件中导入时,我的外部js文件将无效。但是当我在html文件下创建jQuery脚本时,它可以工作。我只是想保存一些代码。
<!doctype html>
<html>
<head>
</head>
<body>
<div id = "container">
<h1>Register</h1>
<hr>
<form method = "post" action = "../process/registerProcess.php" >
<fieldset>
<div class = "form-field">
<label for = "username">Username:</label>
<input type = "text" name = "username" required>
</div>
<div class="form-field">
<label for = "userPassword">Password:</label>
<input type="password" id="userPassword">
</div>
<div class="form-field">
<label for = "userConfirmPassword">Confirm Password:</label>
<input type="password" id="userConfirmPassword" onChange="checkPasswordMatch();">
</div>
<div class="registrationFormAlert" id="divCheckPasswordMatch"> </div>
<div class = "form-field">
<input type = "submit" name = "registerSubmit" value = "Register">
</div>
<div class = "form-field">
<input type = "reset" name = "registerReset" value = "Reset">
</div>
</fieldset>
</form>
</div>
<script type = "text/javascript" src = "jQuery Compressed/jquery.js"></script> //jQuery Compressed
<script type = "text/javascript" src = "register/confirmPassword.js"></script>
</body>
</html>
confirmPassword.js
function checkPasswordMatch()
{
var password = $("#userPassword").val(); //Grab the value of userPassword.
var confirmPassword = $("#userConfirmPassword").val();
if (password != confirmPassword)
{
$("#divCheckPasswordMatch").html("Passwords do not match!");
}
else
{
$("#divCheckPasswordMatch").html("Passwords match.");
}
}
$(document).ready(function ()
{
$("#userConfirmPassword").keyup(checkPasswordMatch);
});
答案 0 :(得分:1)
如果我理解正确,你说你的confirmPassword.js
文件在没有jQuery的情况下不起作用,对吗?
您在confirmPassword.js
文件中使用jQuery,因此必须导入jQuery。