2016-05-18 76 views
0

我創建了一個包含鍵盤事件的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); 
}); 

+1

你爲什麼要在你的外部文件中指定html和'.keyup'指定'onChange'處理程序? –

回答

1

如果我的理解是否正確,你說你的confirmPassword.js文件不無jQuery的工作,對不對?

您在confirmPassword.js文件中使用jQuery,因此您必須導入jQuery。

編輯:私人聊天后發現問題。 JS文件的路徑不正確。

+0

是的。我已經導入了我的jQuery。 'jQuery Compressed/jquery.js' – Francisunoxx

+0

好吧,那還有問題嗎? – tedcurrent

+0

是的,其實我的jQuery鍵盤事件不會在我的HTML。 – Francisunoxx

相關問題