英文:
validation for form is working,but popup box is not working HTML,Javascript
问题
I want a popup box with link to home page by clicking submit button only after validation.
如何在HTML和JavaScript中创建一个仅在验证通过后,通过单击提交按钮触发的弹出框,其中包含指向主页的链接?
Validation is working in this form but popbox is not working. I can't find why this is not working.
此表单中的验证功能正常,但弹出框不起作用。我找不到原因为什么不起作用?
英文:
i want a popup box with link to home page by clicking submit button only after validation.
How to create a popup box with a link to home page, triggered by a submit button only after validation in HTML, and JavaScript?
validation is working in this form but popbox is not working.
i cnt find why this is not working?
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
var subObject = {
"place1": ["place1", "place2", "place3"],
"place2": ["place4", "place4"],
}
window.onload = function() {
var distSel = document.getElementById("district");
var citySel = document.getElementById("city");
for (var x in subObject) {
distSel.options[distSel.options.length] = new Option(x, x);
}
distSel.onchange = function() {
citySel.length = 1;
var z = subObject[distSel.value];
for (var i = 0; i < z.length; i++) {
citySel.options[citySel.options.length] = new Option(z[i], z[i]);
}
}
}
function valid() {
var modal = document.getElementById("myModal");
var btn = document.getElementById("myBtn");
var span = document.getElementsByClassName("close")[0];
if (f1.username.value == "") {
alert('enter the name');
f1.username.focus();
return false;
} else if (f1.date.value == "") {
alert('enter your date of birth');
f1.date.focus();
return false;
} else if (f1.gender.value == "") {
alert('Select gender');
f1.gender.focus();
return false;
} else if (f1.district.value == "") {
alert('select your district');
f1.district.focus();
return false;
} else if (f1.city.value == "") {
alert('select your city');
f1.city.focus();
return false;
} else {
btn.onclick = function() {
modal.style.display = "block";
}
span.onclick = function() {
modal.style.display = "none";
}
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
}
}
<!-- language: lang-css -->
body {
font-family: Arial, Helvetica, sans-serif;
}
form {
border: 3px solid #f1f1f1;
}
input[type=text],
input[type=date] {
width: 75%;
padding: 5px 10px;
margin: 8px 0;
display: inline-block;
border: 1px solid #ccc;
box-sizing: border-box;
}
button {
background-color: #FF5733;
color: white;
padding: 10px 15px;
margin: 8px 0;
border: none;
cursor: pointer;
width: 100px;
border-radius: 15px;
}
button:hover {
opacity: 0.8;
}
select {
height: 30px;
width: 75%;
padding: 0 15px;
text-align: center;
}
form {
padding-left: 290px;
}
body {
font-family: Arial, Helvetica, sans-serif;
}
/* The Modal (background) */
.modal {
display: none;
/* Hidden by default */
position: fixed;
/* Stay in place */
z-index: 1;
/* Sit on top */
padding-top: 100px;
/* Location of the box */
left: 0;
top: 0;
width: 100%;
/* Full width */
height: 100%;
/* Full height */
overflow: auto;
/* Enable scroll if needed */
background-color: rgb(0, 0, 0);
/* Fallback color */
background-color: rgba(0, 0, 0, 0.4);
/* Black w/ opacity */
}
/* Modal Content */
.modal-content {
position: relative;
background-color: #fefefe;
margin: auto;
padding: 0;
border: 1px solid #888;
width: 50%;
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
-webkit-animation-name: animatetop;
-webkit-animation-duration: 0.4s;
animation-name: animatetop;
animation-duration: 0.4s
}
/* Add Animation */
@-webkit-keyframes animatetop {
from {
top: -300px;
opacity: 0
}
to {
top: 0;
opacity: 1
}
}
@keyframes animatetop {
from {
top: -300px;
opacity: 0
}
to {
top: 0;
opacity: 1
}
}
/* The Close Button */
.close {
color: white;
float: right;
font-size: 28px;
font-weight: bold;
}
.close:hover,
.close:focus {
color: #000;
text-decoration: none;
cursor: pointer;
}
.modal-header {
padding: 10px 15px;
background-color: #5cb85c;
color: white;
}
.modal-body {
padding: 10px 15px;
text-align: center;
}
<!-- language: lang-html -->
<!--model div open-->
<div id="myModal" class="modal">
<div class="modal-content">
<div class="modal-header">
<h2>Application Accepted</h2>
<span class="close">&times;</span>
</div>
<div class="modal-body">
<p>Go Back To Home Page <a href="home">Home</a></p>
</div>
</div>
</div>
<!--model div close-->
Form code:
<form method="post" name="f1" action="">
<h2 style="padding-left:50px">Application Form</h2>
<table>
<tr>
<td>
<label for="uname">Name</label>
</td>
<td>
<input type="text" id="uname" name="username" required>
</td>
</tr>
<tr>
<td>
<label for="date">Date Of Birth</label>
</td>
<td>
<input type="date" id="date" name="date" required> </td>
</tr>
<tr>
<td>
<label>Gender</label>
</td>
<td>
<label>
<input type="radio" value="female" name="gender" required> Female</label>
<label>
<input value="male" type="radio" name="gender"> Male</label>
</td>
</tr>
<tr>
<td>
<label for="district">District</label>
</td>
<td>
<select name="district" id="district">
<option value="">---Select District---</option>
</select>
</td>
</tr>
<tr>
<td>
<label for="city">City</label>
</td>
<td>
<select name="city" id="city">
<option value="">---Select District First---</option>
</select>
</td>
</tr>
<tr>
<td></td>
<td>
<button type="submit" id="myBtn" class="cancelbtn" onclick="return valid()">SUBMIT</button>
</td>
</tr>
</table>
</form>
<!-- end snippet -->
答案1
得分: 1
在您的valid
函数中,您只需要在else
部分添加事件处理程序来显示模态框。以下是相应的代码部分:
function valid() {
if (f1.username.value == "") {
alert('请输入姓名');
f1.username.focus();
return false;
} else if (f1.date.value == "") {
alert('请输入您的出生日期');
f1.date.focus();
return false;
} else if (f1.gender.value == "") {
alert('请选择性别');
f1.gender.focus();
return false;
} else if (f1.district.value == "") {
alert('请选择您的区域');
f1.district.focus();
return false;
} else if (f1.city.value == "") {
alert('请选择您的城市');
f1.city.focus();
return false;
} else {
modal.style.display = "block";
return false; // 在这里添加return false,因为此时表单会尝试刷新和提交。
}
}
请注意,我只翻译了代码部分,没有包括其他内容。
英文:
You are adding event handlers in your else part in valid function you just need to show the modal in the else.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
var subObject = {
"place1": ["place1", "place2", "place3"],
"place2": ["place4", "place4"],
}
window.onload = function() {
var distSel = document.getElementById("district");
var citySel = document.getElementById("city");
for (var x in subObject) {
distSel.options[distSel.options.length] = new Option(x, x);
}
distSel.onchange = function() {
citySel.length = 1;
var z = subObject[distSel.value];
for (var i = 0; i < z.length; i++) {
citySel.options[citySel.options.length] = new Option(z[i], z[i]);
}
}
}
var modal = document.getElementById("myModal");
var btn = document.getElementById("myBtn");
var span = document.getElementsByClassName("close")[0];
span.onclick = function() {
modal.style.display = "none";
}
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
function valid() {
if (f1.username.value == "") {
alert('enter the name');
f1.username.focus();
return false;
} else if (f1.date.value == "") {
alert('enter your date of birth');
f1.date.focus();
return false;
} else if (f1.gender.value == "") {
alert('Select gender');
f1.gender.focus();
return false;
} else if (f1.district.value == "") {
alert('select your district');
f1.district.focus();
return false;
} else if (f1.city.value == "") {
alert('select your city');
f1.city.focus();
return false;
} else {
modal.style.display = "block";
return false; // added return false here because form will try to refresh and submit at this point.
}
}
<!-- language: lang-css -->
body {
font-family: Arial, Helvetica, sans-serif;
}
form {
border: 3px solid #f1f1f1;
}
input[type=text],
input[type=date] {
width: 75%;
padding: 5px 10px;
margin: 8px 0;
display: inline-block;
border: 1px solid #ccc;
box-sizing: border-box;
}
button {
background-color: #FF5733;
color: white;
padding: 10px 15px;
margin: 8px 0;
border: none;
cursor: pointer;
width: 100px;
border-radius: 15px;
}
button:hover {
opacity: 0.8;
}
select {
height: 30px;
width: 75%;
padding: 0 15px;
text-align: center;
}
form {
padding-left: 290px;
}
body {
font-family: Arial, Helvetica, sans-serif;
}
/* The Modal (background) */
.modal {
display: none;
/* Hidden by default */
position: fixed;
/* Stay in place */
z-index: 1;
/* Sit on top */
padding-top: 100px;
/* Location of the box */
left: 0;
top: 0;
width: 100%;
/* Full width */
height: 100%;
/* Full height */
overflow: auto;
/* Enable scroll if needed */
background-color: rgb(0, 0, 0);
/* Fallback color */
background-color: rgba(0, 0, 0, 0.4);
/* Black w/ opacity */
}
/* Modal Content */
.modal-content {
position: relative;
background-color: #fefefe;
margin: auto;
padding: 0;
border: 1px solid #888;
width: 50%;
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
-webkit-animation-name: animatetop;
-webkit-animation-duration: 0.4s;
animation-name: animatetop;
animation-duration: 0.4s
}
/* Add Animation */
@-webkit-keyframes animatetop {
from {
top: -300px;
opacity: 0
}
to {
top: 0;
opacity: 1
}
}
@keyframes animatetop {
from {
top: -300px;
opacity: 0
}
to {
top: 0;
opacity: 1
}
}
/* The Close Button */
.close {
color: white;
float: right;
font-size: 28px;
font-weight: bold;
}
.close:hover,
.close:focus {
color: #000;
text-decoration: none;
cursor: pointer;
}
.modal-header {
padding: 10px 15px;
background-color: #5cb85c;
color: white;
}
.modal-body {
padding: 10px 15px;
text-align: center;
}
<!-- language: lang-html -->
<!--model div open-->
<div id="myModal" class="modal">
<div class="modal-content">
<div class="modal-header">
<h2>Application Accepted</h2>
<span class="close">&times;</span>
</div>
<div class="modal-body">
<p>Go Back To Home Page <a href="home">Home</a></p>
</div>
</div>
</div>
<!--model div close-->
<form method="post" name="f1" action="">
<h2 style="padding-left:50px">Application Form</h2>
<table>
<tr>
<td>
<label for="uname">Name</label>
</td>
<td>
<input type="text" id="uname" name="username" required>
</td>
</tr>
<tr>
<td>
<label for="date">Date Of Birth</label>
</td>
<td>
<input type="date" id="date" name="date" required> </td>
</tr>
<tr>
<td>
<label>Gender</label>
</td>
<td>
<label>
<input type="radio" value="female" name="gender" required> Female</label>
<label>
<input value="male" type="radio" name="gender"> Male</label>
</td>
</tr>
<tr>
<td>
<label for="district">District</label>
</td>
<td>
<select name="district" id="district">
<option value="">---Select District---</option>
</select>
</td>
</tr>
<tr>
<td>
<label for="city">City</label>
</td>
<td>
<select name="city" id="city">
<option value="">---Select District First---</option>
</select>
</td>
</tr>
<tr>
<td></td>
<td>
<button type="submit" id="myBtn" class="cancelbtn" onclick="return valid();">SUBMIT</button>
</td>
</tr>
</table>
</form>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论