gpt4 book ai didi

下拉菜单和文本区域的 Javascript 验证

转载 作者:行者123 更新时间:2023-12-03 07:49:10 26 4
gpt4 key购买 nike

我要设置 Javascript 来验证所需的表单字段。如果表单字段丢失,用户屏幕上将出现一个弹出窗口。我已经能够弄清楚大部分内容,但问题出在复选框上。如果用户没有单击复选框,他们将收到错误消息。按照我编写代码的方式,如果用户检查“星期一”,则处理表单,但如果他们检查除星期一以外的任何其他日期,那么他们仍然会收到错误消息,有什么想法吗?

function checked() {
var userName = document.getElementById("text").value;
var userPhone = document.getElementById("phone").value;
var checkBoxCheck = document.getElementById("checkers").checked;
var otherOne = document.getElementById("dropdown").value;
var addInfo = document.getElementById("textarea").value;
if (userName == "" || userPhone == "" || checkBoxCheck == "") {
alert("Please fill in: name, phone number, days of week.");
if (otherOne == "Other") {
if (addInfo == "") {
alert("Please fill in your additional information.");
}
}
return false;
} else {
return true;
}
}
<body>
<div class="container-fluid">
<div class="page-header">
<table border="1" width="100%" Frame="below">
<thead>
<h1 style="color:purple" align="center"><span>C</span>aFe 80's</h1>
</thead>
</table>
</div>
<table border="0" width="20%" cellspacing="20">
<ul class="list-inline nav nav-tabs">
<li><a href="Lab-9CSSHomePageV6.html"><span class="glyphicon glyphicon-home"></span></a>
</li>
<li><a href="Lab-9CSSMenuPageV6.html">Menu</a>
</li>
<li class="active"><a href="Lab-9CSSContactPageV6.html">Contact Us</a>
</li>
</ul>
</table>
</div>
<div class="container" style="height: 700px; position: relative; top: 60px">
<form onsubmit="return checked()" action="lab-9CSSContactPageV6.html" method="POST">
<div class="form-group">
<label for="text">Name:</label>
<input type="text" class="form-control" id="text">
</div>
<div class="form-group">
<label for="email">Email address:</label>
<input type="email" class="form-control" id="email">
</div>
<div class="form-group">
<label for="phone">Phone Number:</label>
<input type="phone" class="form-control" id="phone">
</div>
<div class="form-group">
<label for="dropdown">Reason for Inquiry:</label>
<select class="form-control" id="dropdown">
<option>Catering</option>
<option>Private Party</option>
<option>Feedback</option>
<option id="oOther">Other</option>
</select>
</div>
<div class="form-group">
<label for="textarea">Addition Imformation:</label>
<textarea class="form-control" rows="5" id="textarea"></textarea>
</div>
<div class="form-group" style="position:relative; right: 40px">
<div class="radio-inline">
<label class="radio-inline">Have you been to the restaurant?</label>
<label class="radio-inline">
<input type="radio" name="rad" checked>No</label>
<label class="radio-inline">
<input type="radio" name="rad">Yes</label>
</div>
</div>
<div class="form-group" style="position:relative; right: 40px">
<div class="checkbox-inline">
<label class="checkbox-inline">Best days to contact you:</label>
<label class="checkbox-inline">
<input id="checkers" type="checkbox" value="">M</label>
<label class="checkbox-inline">
<input id="checkers" type="checkbox" value="">T</label>
<label class="checkbox-inline">
<input id="checkers" type="checkbox" value="">W</label>
<label class="checkbox-inline">
<input id="checkers" type="checkbox" value="">TH</label>
<label class="checkbox-inline">
<input id="checkers" type="checkbox" value="">F</label>
</div>
</div>
<div class="form-group" align="center">
<button type="submit" class="btn btn-default">Send Request</button>
</div>
</form>
</div>

最佳答案

多个元素不应具有相同的id。对于您的复选框,您需要使用 name 属性。请注意,您还应该为 value 属性提供一个字符串,否则在提交表单时将不会提交任何内容。

<input id="checkers_m" name="checkers" type="checkbox" value="M">M</input>

然后在验证函数中使用以下方法获取所有复选框元素:

var checkboxes = document.getElementsByName('checkers');

并确保检查其中之一。

关于下拉菜单和文本区域的 Javascript 验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35077038/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com