javascript - 提交时表单不会显示警报
问题描述
因此,我创建了一个表单,该表单需要提醒用户表单已提交,而无需在任何地方实际提交表单。我尝试在单击提交按钮时设置警报,但警报不会显示。我不确定我做错了什么。这是我关于表单的 html 和 .js 代码。
var validName = false;
var validEmail = false;
var validPhone = false;
var validtxtArea = false;
function validate() {
if (validName && validEmail && validPhone && validtxtArea) {
alert("Thank you for submitting your form!");
console.log("Thank you for submitting your form!");
}
}
document.getElementById("Name").addEventListener('input', function(evt) {
var name = document.getElementById("Name").value;
validName = (name !== '');
validate();
});
document.getElementById("Email").addEventListener('input', function(evt) {
var email = document.getElementById("Email").value;
// validEmail = string.indexOf('@') !== -1 && string.indexOf('.') !== -1; //
validate();
});
document.getElementById("Phone").addEventListener('input', function(evt) {
var phone = document.getElementById("Phone").value;
validate();
});
document.getElementById("txtArea").addEventListener('input', function(evt) {
var txtarea = document.getElementById("txtArea").value;
validtxtArea = (txtarea !== '');
validate();
});
<form name="form" id="form" onsubmit="return validate()" method="post" class="text-center border border-light">
<div class="form-group row">
<label for "Name" class="col-md-2 col-form-label">Name:</label>
<div class="col-md-10">
<input type="text" id="Name" name="Name" class="form-control" required> </div>
</div>
<div class="form-group row">
<label for "Email" class="col-md-2 col-form-label">Email:</label>
<div class="col-md-10">
<input type="email" id="Email" name="Email" class="form-control" required> </div>
</div>
<div class="form-group row">
<label for "Phone" class="col-md-2 col-form-label">Phone:</label>
<div class="col-md-10">
<input type="tel" id="Phone" name="Phone" class="form-control" minlength=10 maxlength=10 required> </div>
</div>
<div class="form-group row">
<label for "Inquiry" class="col-form-label">Reason for Inquiry:</label>
<div class="col">
<select name="Reasons" id="inquiry" class="form-control">
<option value="CateringDefault">Catering</option>
<option value="PrivateParty">Private Party</option>
<option value="Feedback">Feedback </option>
<option value="Other">Other</option>
</select>
</div>
</div>
<div class="form-group row">
<label for "info" id="Info" class="col-form-label">Additional Information:</label>
<div class="col">
<textarea class="form-control" id="txtArea"></textarea></div>
</div>
<div class="form-group row">
<label for "Customer" class="col-form-label">Have you been to the restaurant?</label>
<div class="col">
<div class="form-check form-check-inline">
<input type="radio" id="no" name="answer" value="NO" class="form-check-input" checked />
<label class="form-check-label" for="no">No</label></div>
<div class="form-check form-check-inline">
<input type="radio" id="yes" name="answer" value="YES" class="form-check-input" />
<label class="form-check-label" for="yes">Yes</label>
</div>
</div>
<div class="form-group row">
<label for "Choices" class="col-form-label">Best days to contact you:</label>
<div class="col">
<div class="form-check-inline">
<label class="form-check-label" for="M">M </label>
<input type="checkbox" id="choices" name="choices" value="monday" class="form-check-input">
<label class="form-check-label" for="T">T </label>
<input type="checkbox" id="choices" name="choices" value="tuesday" class="form-check-input">
<label class="form-check-label" for="W">W </label>
<input type="checkbox" id="choices" name="choices" value="wednesday" class="form-check-input">
<label class="form-check-label" for="Th">Th </label>
<input type="checkbox" id="choices" name="choices" value="thursday" class="form-check-input">
<label class="form-check-label" for="F">F</label>
<input type="checkbox" id="choices" name="choices" value="friday" class="form-check-input">
</div>
</div>
</div>
</div>
<div class="row">
<button type="submit" id="submitButton" class="btn btn-default" onclick="validate();">Submit</button>
</div>
</form>
解决方案
您是否尝试通过将验证功能绑定到提交按钮来一次验证所有字段?这可能比验证每个键入的字符更好。
也许
function validate(name, email, phone, txtarea) {
if (name !== "" && email !== "" && phone !== "" && txtarea !== "") {
alert("Thank you for submitting your form!");
console.log("Thank you for submitting your form!");
}
}
document.getElementById("submitButton").addEventListener('click', function(evt) {
evt.preventDefault()
var email = document.getElementById("Email").value;
var name = document.getElementById("Name").value;
var phone = document.getElementById("Phone").value;
var txtarea = document.getElementById("txtArea").value;
validate(name, email, phone, txtarea);
});
我希望这会有所帮助
推荐阅读
- curl - curl 调用 webrequest 转换
- git - 使用本地更改镜像 Git 存储库
- .htaccess - 如何通过 htaccess 重定向到新的 URL 结构
- c# - 如何在整个程序中继续使用相同的 List<>?
- php - 为什么codeignater视图会加载所有html代码作为响应
- hadoop - 无法远程调试 mapreduce 作业的原因
- assembly - 如何验证STM8S控制器中代码的内存校验和
- python - Matplotlib 透明点在透明线上
- javascript - Node.js,MongoDB 错误 - “消息”:“模式尚未注册模型 \“类别”。\n使用 mongoose.model(名称,模式)”
- c++ - 减少已经使用 Git 的编译时间