gpt4 book ai didi

javascript - 单击警报确定后如何留在同一位置?

转载 作者:行者123 更新时间:2023-11-30 12:12:19 27 4
gpt4 key购买 nike

我的问题是,当我点击警告消息确定按钮时,页面转到其他页面 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)" >

关于javascript - 单击警报确定后如何留在同一位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33416226/

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