简体   繁体   English

单击警报后如何留在同一位置?

[英]How to stay in the same location after click on alert ok?

my problem is when i am clicking on alert message ok button the page goes on other page sample_page.php , and either checkbox or radio filed is remain empty.. i just need to stay on the same page, when i will check the radio field or checkbox field then only page goes on the other page otherwise it will stay remain on same page... 我的问题是,当我点击提示消息确定按钮页面进入其他页面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)" >

您应该在onClick中添加函数调用的返回:

<input type="submit" name="submit" value="Submit" onClick="return ValidateForm(this.form)" >

please change your some code as below. 请更改您的一些代码,如下所示。 in form tag replace onclick tag with 在表单标签中替换onclick标签

onClick="return ValidateForm(this.form)" 

and replace 并替换

return   alert("No Checkbox is checked11");

with

alert("No Checkbox is checked11");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM