我整天都在做这项任务,似乎无法看出我出错的地方。观看了一些YouTube视频并搜索了几天,密切关注,但仍然遗漏了一些东西。
我应该使用Javascript来验证联系我们表单。以下是说明:
function checkForm() {
var name = document.getElementById("nameInput").value;
var email = document.getElementById("emailInput").value;
var phone = document.getElementById("phoneInput").value;
var inquirySelect = document.getElementById("dropdown").value;
var addInfoBox = document.getElementById("textInput").value;
if (name == "") {
alert("Name is required");
return false;
} else if (email == "" && phone == "") {
alert("Please provide your email address or a phone number");
return false;
} else if (inquirySelect == "Other" && addInfoBox == "") {
alert("Please add additional info");
return false;
}
}

<form name="contactForm" action="submission.html" method="POST" onsubmit="return checkForm()">
<table id="form">
<tr>
<td id="question">Name:</td>
<td id="answer">
<input type="text" name="Name" id="nameInput">
</td>
<td id="question"></td>
</tr>
<tr>
<td id="question">Email:</td>
<td id="answer">
<input type="text" name="Email" id="emailInput">
</td>
</tr>
<tr>
<td id="question">Phone:</td>
<td id="answer">
<input type="text" name="phoneNumber" id="phoneInput">
</td>
</tr>
<tr>
<td id="question">Reason for Inquiry:</td>
<td id="answer">
<select name="reason" id="dropdown">
<option value="Catering">Catering</option>
<option value="Private Party">Private Party</option>
<option value="Feedback">Feedback</option>
<option value="Other">Other</option>
</select>
</td>
</tr>
<tr>
<td id="question">Additional Information:</td>
<td id="answer">
<textarea name="additionalInfo" id="textInput">
</textarea>
</td>
</tr>
<tr>
<td id="question">Have you been to the restaurant?:</td>
<td id="answer">
<input type="radio" name="answer" value="NO" checked>No
<input type="radio" name="answer" value="YES">Yes</td>
</tr>
<tr>
<td id="question">Best days to contact you:</td>
<td id="answer">
<input type="checkbox" name="check" value="1">M
<input type="checkbox" name="check" value="2">T
<input type="checkbox" name="check" value="3">W
<input type="checkbox" name="check" value="4">Th
<input type="checkbox" name="check" value="5">F</td>
</tr>
<tr>
<td></td>
<td id="answer">
<input type="submit" value="Send Request">
</td>
</tr>
</table>
</form>
&#13;
到目前为止,我能够让第1步工作。但是,我似乎无法让步骤2返回错误,它将始终通过。
我还没有开始完成最后一步,但我知道我将使用for循环来检查已检查的值,如果没有选中则返回false。希望我不会因为那个lol而回到这里。感谢您的帮助,谢谢
答案 0 :(得分:3)
清除标签textarea之间的空格:
您的代码是:
<textarea name="additionalInfo" id="textInput">
</textarea></td>
改变如下:
<textarea name="additionalInfo" id="textInput"></textarea></td>
为我工作。