我創建了一個包含鍵盤事件的jQuery外部文件confirmPassword.js
,但是當我試圖在我的html文件中導入時,我的外部js文件將無法工作。但是,當我在html文件下創建jQuery腳本時它工作。我只是想保存一些代碼行。導入jQuery腳本將無法在HTML文件上工作
<!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);
});
你爲什麼要在你的外部文件中指定html和'.keyup'指定'onChange'處理程序? –