爲什麼我的條件語句無法正常工作?我想同時顯示錯誤消息。在表單驗證中同時顯示所有錯誤消息
function validate() {
if (firstName.value == "") {
document.getElementById('error').innerHTML = "*Field is empty";
return false;
} else if (lastName.value == "") {
document.getElementById('errorTwo').innerHTML = "*Field is empty";
return false;
} else {
return true;
}
}
<form name="form" action="action.php" method="post" onsubmit="return validate()">
<div class="wrapper">
<span>Name</span>
<br>
<input type="text" name="firstName" id="firstName" placeholder="First Name" onfocus="this.placeholder=''" onblur="this.placeholder='First Name'" />
<label id="error"></label>
<br>
<input type="text" name="middleName" id="middleName" placeholder="Middle Name (optional)" onfocus="this.placeholder=''" onblur="this.placeholder='Middle Name (optional)'" />
<input type="text" name="lastName" id="lastName" placeholder="Last Name" onfocus="this.placeholder=''" onblur="this.placeholder='Last Name'" />
<label id="errorTwo"></label>
<br>
<br>
</div>
你錯過了關閉'}'。您是否在[JavaScript控制檯]中看到任何錯誤(http://webmasters.stackexchange.com/questions/8525/how-to-open-the-javascript-console-in-different-browsers)? – 2015-03-31 14:49:36
您已將腳本標記粘貼到HTML標記範圍之外,這是您的第一個問題,第二個問題是您尚未聲明姓氏或姓氏 – Brad 2015-03-31 14:54:27
對不起,我只是沒有將它包含在我的帖子中,但我已經關閉在我的實際代碼..我沒有任何錯誤,它只是不工作的方式,我想它是 – 2015-03-31 14:54:36