我正在尝试让Javascript检查所需的字段:名称,电话号码,至少选中一个复选框,如果下拉菜单位于“其他”,则需要填写附加信息字段。如果用户没有输入姓名和电话号码字段,我已经能够弄清楚如何显示弹出窗口,但我的问题是:
如果用户没有填写附加信息框,我无法显示弹出窗口,具体取决于下拉菜单是否设置为“其他”,如果复选框,我也无法显示弹出窗口没有检查,最后,即使填写了必填字段,它仍然会给我一个弹出错误消息..任何建议?
<script>
var userInput = document.getElementById("text").value;
var userPhone = document.getElementById("phone").value;
var dropDownBox = document.getElementById("dropdown").value;
var addInfo = document.getElementById("textarea").value;
var otherOne = document.getElementById("oOther").value;
var checkBoxCheck = document.getElementById("checkers").checked;
function checking(){
if(userInput === undefined){
alert("Please fill in your name.");
}
if(userPhone === undefined){
alert("Please fill in your phone number.");
return false;
} else{
return true;
}
if(dropDownBox === "Other"){
if(addInfo === undefined){
alert("Please fill in your additional information.")
}
}
if(checkBoxCheck===undefined){
alert("Please select a day of the week to contact you.");
}
}
</script>
</head>
<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 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 onclick="return checking()"type="submit" class="btn btn-default">Send Request</button>
</div>
</form>
</div>
答案 0 :(得分:0)
我知道你已经弄清楚了问题的第一部分,并且正在回答第二部分。请注意你需要第一部分,请告诉我。
我修改了您的下拉列表以获得值。
<select class="form-control" id="dropdown">
<option value="1">Catering</option>
<option value="2">Private Party</option>
<option value="3">Feedback</option>
<option value="4">Other</option>
</select>
在您的javascript中,添加此功能而不是当前的
if(document.getElementById("dropdown").value=="4")
{
alert("Other");
if(addInfo=="" ||addInfo==undefined)
alert("fill in the text area");
return false;
}
}
对于日期部分,这是代码:
<强> JS 强>
if(document.getElementById("checkers1").checked===true||
document.getElementById("checkers2").checked===true||
document.getElementById("checkers3").checked===true||
document.getElementById("checkers4").checked===true||
document.getElementById("checkers5").checked===true){
return true;
}
else{
alert("Please select a day");
return false;
}
<强> HTML 强>
<label class="checkbox-inline"><input id="checkers1" type="checkbox" value="">M</label>
<label class="checkbox-inline"><input id="checkers2" type="checkbox" value="">T</label>
<label class="checkbox-inline"><input id="checkers3" type="checkbox" value="">W</label>
<label class="checkbox-inline"><input id="checkers4" type="checkbox" value="">T</label>
<label class="checkbox-inline"><input id="checkers5" type="checkbox" value="">F</label>