當我在HTML運行我的JavaScript代碼,這很好,但是當我在本地主機上運行相同的代碼,它顯示我在控制檯此消息:的Javascript不是在XAMPP工作
Uncaught TypeError: Cannot read property 'style' of null
這是我的代碼
代碼<form method="post">
<ul id="edit">
<li>
<input type="text" name="adress" id="adress" placeholder="Adresa" />
</li>
<li>
<input type="text" name="city" id="city" placeholder="Grad" />
</li>
<li>
<input type="number" name="zip" id="zip" placeholder="Poštanski broj" />
</li>
<li>
<input type="tel" name="phone_number" id="phone_number" placeholder="Tel. broj" />
</li>
<li>
<input type="password" name="password" id="password" placeholder="Lozinka" />
<div class="password_strength" id="password_strength"></div>
</li>
<li>
<input type="password" name="repassword" id="repassword" placeholder="Ponoviti lozinku" onkeyup="checkPass(); return false;" />
</li><div id="confirmMessage"></div>
<br>
<li>
<input type="submit" name="submit" id="submit" value="Izmjeni" />
</li>
<br>
</ul>
</form>
<script>
function checkPass() {
var password = document.getElementById('password');
var repassword = document.getElementById('repassword');
var message = document.getElementById('confirmMessage');
var goodColor = "#66cc66";
var badColor = "#ff6666";
if (password.value == repassword.value) {
repassword.style.backgroundColor = goodColor;
message.style.color = goodColor;
message.innerHTML = "Passwords Match!"
} else {
repassword.style.backgroundColor = badColor;
message.style.color = badColor;
message.innerHTML = "Passwords Do Not Match!";
}
}
$('#password, #username').keydown(function (e) {
if (e.which == 32) {
return false;
}
});
$('#password').keyup(function() {
var PasswordLength = $(this).val().length;
var PasswordStrength = $('#password_strength');
if (PasswordLength <= 0) {
PasswordStrength.html('');
PasswordStrength.removeClass('normal weak strong verystrong');
}
if (PasswordLength > 0 && PasswordLength < 4) {
PasswordStrength.html('weak');
PasswordStrength.removeClass('normal strong verystrong').addClass('weak');
}
if (PasswordLength > 4 && PasswordLength < 8) {
PasswordStrength.html('Normal');
PasswordStrength.removeClass('weak strong verystrong').addClass('normal');
}
if (PasswordLength >= 8 && PasswordLength < 12) {
PasswordStrength.html('Strong');
PasswordStrength.removeClass('weak normal verystrong').addClass('strong');
}
if (PasswordLength >= 12) {
PasswordStrength.html('Very Strong');
PasswordStrength.removeClass('weak normal strong').addClass('verystrong');
}
});
</script>
第一部分應在綠色顏色添加到我的輸入框repeaedt密碼,如果密碼匹配或紅色,如果他們不這樣做,但它始終是紅色的。
第二部分應該爲密碼強度添加一個類,但什麼都不是。
這裏是'confirmMessage'? –
@HardyMathew我編輯後 – CroVG
你可以在jsfiddle上添加你的代碼,所以我們可以看看? – BaconJuice