导入jQuery脚本获胜'我不处理html文件

Importing jQuery script won't work on html file

本文关键字:处理 html 文件 jQuery 脚本 获胜 导入      更新时间:2023-09-26

我创建了一个包含键盘事件的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);
});

如果我理解正确,你的confirmPassword.js文件在没有jQuery的情况下无法工作,对吧?

您正在confirmPassword.js文件中使用jQuery,因此必须导入jQuery。

编辑:在私人聊天后发现问题。JS文件的路径不正确。