[英]I have an error with audiotracks on javascript, an error with "lenght"
[英]I need to check lenght of password but for some reason .lenght doesnt work?
if()
執行password1.lenghth
並測試長度,它仍然顯示密碼短於 5。var pass_lenght= password1.lenght;
和var pass_lenght= document.getElementById("password1").lenght;
沒有工作 有誰知道為什么?謝謝
var password1= document.getElementById("password1");
var password2= document.getElementById("password2");
password1.addEventListener("blur",function()
{
if (password1!= password2) {
password1.setAttribute("class","wrong");
password2.setAttribute("class","wrong");
return false;
}
else if(password1.lenght<5)
{
password1.setAttribute("class","wrong");
return false;
}
else
{
password1.setAttribute("class","correct");
}
});
password1.addEventListener("blur",function()
{
if (password1!= lozinka2) {
password1.setAttribute("class","wrong");
password2.setAttribute("class","wrong");
return false;
}
else if(password2.lenght<5)
{
password2.setAttribute("class","wrong");
return false;
}
else
{
password2.setAttribute("class","correct");
}
});
似乎問題在於您實際上並未訪問長度。 你的getElementById
完全按照它說的去做,通過 id 獲取一個元素。 但是您想要輸入的值,而不是元素。
You: password1.length
Correct: password1.value.length
此外,即使您更改它,甚至不會到達else if
語句,因此那將毫無意義。
因此,只需將password1
和password2
變量更改為:
var password1= document.getElementById("password1").value;
var password2= document.getElementById("password2").value;
上面的代碼有一些缺陷,如下:
value
屬性。var password1= document.getElementById("password1").value;
length
拼寫錯誤。您通常測量輸入字段的長度,但永遠不能直接測量元素的長度。 您必須通過以下方式獲取輸入字段的值:
var password1= document.getElementById("password1").value;
console.log(password1.length) //======> this will print the length.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.