所以,我正在处理一个带有联系表格的页面,我想让它检查字段是否为空。我在下面使用这个简单的代码,但它只适用于第一个 if。当我点击提交时,它只显示“我想知道你是谁”。它甚至看不到其他 if 语句!
function validation() {
var name = document.getElementById("name").value;
var email = document.getElementById("email").value;
var subject = document.getElementById("subject").value;
var message = document.getElementById("message").value;
if(name == null || name == "") {
document.getElementById("name-error").innerHTML = "I want to know who you are!";
return false;
}
if(email == null || email == "") {
document.getElementById("email-error").innerHTML = "Give me your email!";
return false;
}
if(subject == null || subject == "") {
document.getElementById("subject-error").innerHTML = "I need a subject!";
return false;
}
if(message == null || message == "") {
document.getElementById("message-error").innerHTML = "Don't forget your message!";
return false;
}
}
这是 HTML:
<form name="contact" id="contact" action="./assets/send.php" method="post" id="form">
<label>Full name</label>
<input type="text" name="name" id="name"><span id="name-error"></span>
<label>Email</label>
<input type="text" name="email" id="email"><span id="email-error"></span>
<label>Subject</label>
<input type="text" name="subject" id="subject"><span id="subject-error"></span>
<label>Message</label>
<textarea name="message" id="message"></textarea><span id="message-error"></span>
<button onclick="return validation()" type="submit" name="send" id="send">Send</button>
</form>