我的问题是,当我点击提示消息确定按钮时,页面会进入其他页面sample_page.php,并且复选框或无线电字段仍为空。 我只需要保持在同一页面,当我检查无线电字段或复选框字段时,只有页面在另一页上,否则它将保持在同一页面上...
<!DOCTYPE HTML>
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
<script LANGUAGE="JavaScript">
function ValidateForm(form)
{
ErrorText= "";
var a = document.myForm.fname.value;
if(a=="")
{
alert("Please Enter Your Name");
document.myForm.fname.focus();
return false;
}
if ( ( form.gender[0].checked == false ) && ( form.gender[1].checked == false ) )
{
alert ( "Please choose your Gender: Male or Female" );
return false;
}
var group = document.myForm.Hobby;
for (var i=0; i<group.length; i++)
{
if (group[i].checked)
break;
}
if (i==group.length)
{
return alert("No Checkbox is checked");
return false;
}
if (ErrorText= "") {form.reload() }
}
</script>
</head>
<body>
<h2>PHP Form Validation Example</h2>
<p><span class="error">* required field.</span></p>
<form method="post" name="myForm" action="sample_output.php">
<p>First Name:
<input type="text" name="fname" id="fname" />
<span class="error">* </span>
<br><br>
Last Name: <input type="text" name="lname"/>
<span class="error">*</span>
<br><br>
Gender:
<input type="radio" name="gender" value="female">Female
<input type="radio" name="gender" value="male">Male
<span class="error">*</span> </p>
<p>
<label for="birth">Date Of Birth</label>:
<input type="date" name="birth" id="birth"required>
<span class="error">*</span>
</p>
<p>
<label for="address">Address: </label>
<input type="text" name="address" id="address"required>
<span class="error">*</span>
</p>
<p>
<label for="fields">Country:</label>
<select name="country" id="country" required>
<option value="">Select Country</option>
<option value="INDIA">INDIA</option>
<option value="USA">USA</option>
<option value="UK">UK</option>
<option value="UAE">UAE</option>
<option value="RASSIA">RASSIA</option>
<option value="FRANCE">FRANCE</option>
</select>
</p>
<p>
<label for="state">State: </label>
<select name="state" id="state">
</select>
</p>
<p>
<label for="city">City: </label>
<select name="city" id="city">
</select>
</p>
<p>
<label for="mob_number">Mobile Number: </label>
<input type="text" name="mob_number" id="mob_number">
</p>
<p>
<label for="email">Email: </label>
<input type="text" name="email" id="email">
</p>
<p>Hobbies:
<br>
<label>
<input type="checkbox" name="Hobby" value="checkbox" id="Hobby_0">
Singing</label>
<br>
<label>
<input type="checkbox" name="Hobby" value="checkbox" id="Hobby_1">
Dansing</label>
<br>
<label>
<input type="checkbox" name="Hobby" value="checkbox" id="Hobby_2">
Reading</label>
<br>
<label>
<input type="checkbox" name="Hobby" value="checkbox" id="Hobby_3">
Playing</label>
<br>
<label>
<input type="checkbox" name="Hobby" value="checkbox" id="Hobby_4">
Other</label>
<span class="error"></span>
<br>
<br>
</p>
<input type="submit" name="submit" value="Submit" onClick="ValidateForm(this.form)" >
答案 0 :(得分:2)
您应该在onClick上添加一个返回函数调用:
<input type="submit" name="submit" value="Submit" onClick="return ValidateForm(this.form)" >
答案 1 :(得分:1)
请更改以下代码。在表单标签中用
替换onclick标签onClick="return ValidateForm(this.form)"
并替换
return alert("No Checkbox is checked11");
带
alert("No Checkbox is checked11");