我在提交表单时遇到一个问题,无法检查空白选择字段。我在下面解释我的代码。
添加投诉.php:
<form name="billdata" id="billdata" enctype="multipart/form-data" method="POST" onSubmit="javascript:return checkForm();">
<div class="input-group bmargindiv1 col-md-12">
<span class="input-group-addon ndrftextwidth text-right" style="width:180px"> Name :</span>
<input type="text" name="u_name" id="name" class="form-control" placeholder="Add Name" onKeyPress="clearField('name');">
</div>
<div class="input-group bmargindiv1 col-md-12">
<span class="input-group-addon ndrftextwidth text-right" style="width:180px">Email :</span>
<input type="email" name="u_email" id="emailid" class="form-control" placeholder="Add Email Id" onKeyPress="clearField('emailid');">
</div>
<div class="input-group bmargindiv1 col-md-12">
<span class="input-group-addon ndrftextwidth text-right" style="width:180px">TYPE OF COMPLAINT
:</span>
<select class="form-control" id="compleant" name="complainType" onChange="javascript:checkComField();">
<option value="">Select types of complaint</option>
<option value="card">Card Related</option>
<option value="claim">Claim Related</option>
<option value="product">Product Related</option>
<option value="Premimum">Premimum Related</option>
</select>
</div>
<input type="submit" class="btn btn-success" name="complainSubmit" id="addProfileData" value="Add"/>
</form>
<script>
function checkForm(){
var s=document.billdata;
console.log('compalin',s.complainType.value=='');
if(s.u_name.value==''){
alert('Please enter name');
s.u_name.focus();
s.u_name.style.borderColor = "red";
return false;
}else if(s.u_email.value==''){
alert('Please enter email');
s.u_email.focus();
s.u_email.style.borderColor = "red";
return false;
}else if(! validateEmail(s.u_email.value)){
alert('Please enter a valid email');
s.u_email.focus();
s.u_email.style.borderColor = "red";
return false;
}else if(s.complainType.value==''){
alert('Please select the type of complain');
s.complainType.focus();
s.complainType.style.borderColor = "red";
return false;
}else{
}
}
</script>
在这里,我的问题是当我在名称和电子邮件字段中有一些值并且没有从选择字段中选择值时,然后我单击表单正在提交的添加按钮,但空白选择字段无法检查。在这里,我需要在提交之前检查所有空白字段。请帮助我。
我检查了你的代码。它在控制台中抛出错误。未定义clearField
和validateEmail
的函数。我注释掉了这些功能,它工作正常。
这是相同的演示。
您可以添加clearField
和validateEmail
函数,然后检查您的代码是否正常工作。
在此演示中,我添加了这些功能,并且工作正常。
更改
}else if(s.complainType.value==''){
自
}else if(s.complainType.selectedIndex == 0){
并确保下次发布您的全职时间。
尚未添加脚本标记
<script>
function checkForm(){
var s=document.billdata;
console.log('compalin',s.complainType.value=='');
if(s.u_name.value==''){
alert('Please enter name');
s.u_name.focus();
s.u_name.style.borderColor = "red";
return false;
}else if(s.u_email.value==''){
alert('Please enter email');
s.u_email.focus();
s.u_email.style.borderColor = "red";
return false;
}else if(! validateEmail(s.u_email.value)){
alert('Please enter a valid email');
s.u_email.focus();
s.u_email.style.borderColor = "red";
return false;
}else if(s.complainType.value==''){
alert('Please select the type of complain');
s.complainType.focus();
s.complainType.style.borderColor = "red";
return false;
}else{
}
}
</script>