验证表单已经生效,但弹出框不起作用 HTML,JavaScript

huangapple go评论72阅读模式
英文:

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 = {
  &quot;place1&quot;: [&quot;place1&quot;, &quot;place2&quot;, &quot;place3&quot;],
  &quot;place2&quot;: [&quot;place4&quot;, &quot;place4&quot;],
}


window.onload = function() {

  var distSel = document.getElementById(&quot;district&quot;);
  var citySel = document.getElementById(&quot;city&quot;);

  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 &lt; z.length; i++) {
      citySel.options[citySel.options.length] = new Option(z[i], z[i]);
    }
  }
}

function valid() {

  var modal = document.getElementById(&quot;myModal&quot;);
  var btn = document.getElementById(&quot;myBtn&quot;);
  var span = document.getElementsByClassName(&quot;close&quot;)[0];



  if (f1.username.value == &quot;&quot;) {
    alert(&#39;enter the name&#39;);
    f1.username.focus();
    return false;
  } else if (f1.date.value == &quot;&quot;) {
    alert(&#39;enter your date of birth&#39;);
    f1.date.focus();
    return false;
  } else if (f1.gender.value == &quot;&quot;) {
    alert(&#39;Select gender&#39;);
    f1.gender.focus();
    return false;
  } else if (f1.district.value == &quot;&quot;) {
    alert(&#39;select your district&#39;);
    f1.district.focus();
    return false;
  } else if (f1.city.value == &quot;&quot;) {
    alert(&#39;select your city&#39;);
    f1.city.focus();
    return false;
  } else {



    btn.onclick = function() {
      modal.style.display = &quot;block&quot;;
    }

    span.onclick = function() {
      modal.style.display = &quot;none&quot;;
    }


    window.onclick = function(event) {
      if (event.target == modal) {
        modal.style.display = &quot;none&quot;;
      }
    }

  }
}

<!-- 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 -->

&lt;!--model div open--&gt;

&lt;div id=&quot;myModal&quot; class=&quot;modal&quot;&gt;
  &lt;div class=&quot;modal-content&quot;&gt;
    &lt;div class=&quot;modal-header&quot;&gt;

      &lt;h2&gt;Application Accepted&lt;/h2&gt;
      &lt;span class=&quot;close&quot;&gt;&amp;times;&lt;/span&gt;
    &lt;/div&gt;
    &lt;div class=&quot;modal-body&quot;&gt;
      &lt;p&gt;Go Back To Home Page &lt;a href=&quot;home&quot;&gt;Home&lt;/a&gt;&lt;/p&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;!--model div close--&gt;


Form code:

&lt;form method=&quot;post&quot; name=&quot;f1&quot; action=&quot;&quot;&gt;


  &lt;h2 style=&quot;padding-left:50px&quot;&gt;Application Form&lt;/h2&gt;
  &lt;table&gt;
    &lt;tr&gt;
      &lt;td&gt;
        &lt;label for=&quot;uname&quot;&gt;Name&lt;/label&gt;
      &lt;/td&gt;
      &lt;td&gt;
        &lt;input type=&quot;text&quot; id=&quot;uname&quot; name=&quot;username&quot; required&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;
        &lt;label for=&quot;date&quot;&gt;Date Of Birth&lt;/label&gt;
      &lt;/td&gt;
      &lt;td&gt;
        &lt;input type=&quot;date&quot; id=&quot;date&quot; name=&quot;date&quot; required&gt; &lt;/td&gt;
    &lt;/tr&gt;



    &lt;tr&gt;
      &lt;td&gt;
        &lt;label&gt;Gender&lt;/label&gt;
      &lt;/td&gt;
      &lt;td&gt;
        &lt;label&gt;
                    &lt;input type=&quot;radio&quot; value=&quot;female&quot; name=&quot;gender&quot; required&gt; Female&lt;/label&gt;
        &lt;label&gt;
                    &lt;input value=&quot;male&quot; type=&quot;radio&quot; name=&quot;gender&quot;&gt; Male&lt;/label&gt;
      &lt;/td&gt;
    &lt;/tr&gt;





    &lt;tr&gt;
      &lt;td&gt;
        &lt;label for=&quot;district&quot;&gt;District&lt;/label&gt;
      &lt;/td&gt;
      &lt;td&gt;
        &lt;select name=&quot;district&quot; id=&quot;district&quot;&gt;
          &lt;option value=&quot;&quot;&gt;---Select District---&lt;/option&gt;
        &lt;/select&gt;
      &lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td&gt;
        &lt;label for=&quot;city&quot;&gt;City&lt;/label&gt;
      &lt;/td&gt;
      &lt;td&gt;
        &lt;select name=&quot;city&quot; id=&quot;city&quot;&gt;
          &lt;option value=&quot;&quot;&gt;---Select District First---&lt;/option&gt;
        &lt;/select&gt;
      &lt;/td&gt;
    &lt;/tr&gt;



    &lt;tr&gt;
      &lt;td&gt;&lt;/td&gt;
      &lt;td&gt;




        &lt;button type=&quot;submit&quot; id=&quot;myBtn&quot; class=&quot;cancelbtn&quot; onclick=&quot;return valid()&quot;&gt;SUBMIT&lt;/button&gt;



      &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/table&gt;
&lt;/form&gt;

<!-- 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 = {
  &quot;place1&quot;: [&quot;place1&quot;, &quot;place2&quot;, &quot;place3&quot;],
  &quot;place2&quot;: [&quot;place4&quot;, &quot;place4&quot;],
}


window.onload = function() {

  var distSel = document.getElementById(&quot;district&quot;);
  var citySel = document.getElementById(&quot;city&quot;);

  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 &lt; z.length; i++) {
      citySel.options[citySel.options.length] = new Option(z[i], z[i]);
    }
  }
}

var modal = document.getElementById(&quot;myModal&quot;);
var btn = document.getElementById(&quot;myBtn&quot;);
var span = document.getElementsByClassName(&quot;close&quot;)[0];

span.onclick = function() {
  modal.style.display = &quot;none&quot;;
}

window.onclick = function(event) {
  if (event.target == modal) {
    modal.style.display = &quot;none&quot;;
  }
}

function valid() {
  if (f1.username.value == &quot;&quot;) {
    alert(&#39;enter the name&#39;);
    f1.username.focus();
    return false;
  } else if (f1.date.value == &quot;&quot;) {
    alert(&#39;enter your date of birth&#39;);
    f1.date.focus();
    return false;
  } else if (f1.gender.value == &quot;&quot;) {
    alert(&#39;Select gender&#39;);
    f1.gender.focus();
    return false;
  } else if (f1.district.value == &quot;&quot;) {
    alert(&#39;select your district&#39;);
    f1.district.focus();
    return false;
  } else if (f1.city.value == &quot;&quot;) {
    alert(&#39;select your city&#39;);
    f1.city.focus();
    return false;
  } else {
    modal.style.display = &quot;block&quot;;
    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 -->

&lt;!--model div open--&gt;

&lt;div id=&quot;myModal&quot; class=&quot;modal&quot;&gt;
  &lt;div class=&quot;modal-content&quot;&gt;
    &lt;div class=&quot;modal-header&quot;&gt;

      &lt;h2&gt;Application Accepted&lt;/h2&gt;
      &lt;span class=&quot;close&quot;&gt;&amp;times;&lt;/span&gt;
    &lt;/div&gt;
    &lt;div class=&quot;modal-body&quot;&gt;
      &lt;p&gt;Go Back To Home Page &lt;a href=&quot;home&quot;&gt;Home&lt;/a&gt;&lt;/p&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;!--model div close--&gt;

&lt;form method=&quot;post&quot; name=&quot;f1&quot; action=&quot;&quot;&gt;


  &lt;h2 style=&quot;padding-left:50px&quot;&gt;Application Form&lt;/h2&gt;
  &lt;table&gt;
    &lt;tr&gt;
      &lt;td&gt;
        &lt;label for=&quot;uname&quot;&gt;Name&lt;/label&gt;
      &lt;/td&gt;
      &lt;td&gt;
        &lt;input type=&quot;text&quot; id=&quot;uname&quot; name=&quot;username&quot; required&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;
        &lt;label for=&quot;date&quot;&gt;Date Of Birth&lt;/label&gt;
      &lt;/td&gt;
      &lt;td&gt;
        &lt;input type=&quot;date&quot; id=&quot;date&quot; name=&quot;date&quot; required&gt; &lt;/td&gt;
    &lt;/tr&gt;



    &lt;tr&gt;
      &lt;td&gt;
        &lt;label&gt;Gender&lt;/label&gt;
      &lt;/td&gt;
      &lt;td&gt;
        &lt;label&gt;
          &lt;input type=&quot;radio&quot; value=&quot;female&quot; name=&quot;gender&quot; required&gt; Female&lt;/label&gt;
        &lt;label&gt;
          &lt;input value=&quot;male&quot; type=&quot;radio&quot; name=&quot;gender&quot;&gt; Male&lt;/label&gt;
      &lt;/td&gt;
    &lt;/tr&gt;





    &lt;tr&gt;
      &lt;td&gt;
        &lt;label for=&quot;district&quot;&gt;District&lt;/label&gt;
      &lt;/td&gt;
      &lt;td&gt;
        &lt;select name=&quot;district&quot; id=&quot;district&quot;&gt;
          &lt;option value=&quot;&quot;&gt;---Select District---&lt;/option&gt;
        &lt;/select&gt;
      &lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td&gt;
        &lt;label for=&quot;city&quot;&gt;City&lt;/label&gt;
      &lt;/td&gt;
      &lt;td&gt;
        &lt;select name=&quot;city&quot; id=&quot;city&quot;&gt;
          &lt;option value=&quot;&quot;&gt;---Select District First---&lt;/option&gt;
        &lt;/select&gt;
      &lt;/td&gt;
    &lt;/tr&gt;



    &lt;tr&gt;
      &lt;td&gt;&lt;/td&gt;
      &lt;td&gt;




        &lt;button type=&quot;submit&quot; id=&quot;myBtn&quot; class=&quot;cancelbtn&quot; onclick=&quot;return valid();&quot;&gt;SUBMIT&lt;/button&gt;



      &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/table&gt;
&lt;/form&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月24日 19:26:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76323030.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定