弹出警报过早显示?

时间:2014-04-05 13:16:53

标签: javascript html url-redirection alerts

我正在处理一个包含一些必填字段的提交表单,并且我有一段代码,允许弹出菜单一旦您点击"提交"按钮,一旦你点击'#34; ok"在警报上,您被重定向到主页。这是我弹出警报的代码。

<div id="popup">
    <button onclick="myFunction()">Submit</button>
        <script>
            function myFunction(){
                alert("Thank you! \nYour sumbission has been 
                accepted and you will receive a conformation email shortly! \n
                You will now be taken to the Home page.");}
        </script> <input type="reset" value="Reset" />
</div>

这是将您重定向到主页的代码的开头。

<div id="all">
    <div id="text"> 
        <h1>Your Information</h1>
            <form id="contact_form" action="home.html" method="post">

            <input type="hidden" name="redirect" value="home.html" />

(注意:我剪切了上面的代码段,因为以下信息只是表单输入框)

这就像你想象的那样,一旦你点击提交按钮就会弹出警报。但是我的问题是我的表单上有一些必填字段,如果字段没有填写并且您点击提交&#34;,则会出现弹出提示,一旦您点击&#34;确定&# 34;在警报上,因为你还没有填写这些字段,它会把你带回到表格和一个小框,说'#34;这个字段是必需的&#34;出现。

这是错误的顺序。我希望所需字段的警告框在弹出警报之前显示以确认表单提交,这样如果您有必填字段并点击提交,它将告诉您填写必填字段在消息出现之前感谢您提交。

这可能是一个简单的放置修复或添加一些代码,但我很难弄清楚如何做或找到一个可以帮助我的例子。

如果你知道该怎么做,我真的很感激,谢谢!

编辑:这是验证码的javascript文件。

window.onload = setForm;

function setForm() {
   document.forms[0].onsubmit = function() {
      if (this.checkValidity()) alert("No invalid data detected. Will 
      retain data for further testing.");
      return false;
   }
}

2 个答案:

答案 0 :(得分:0)

function Validation()
{
vak k = true;

 k = function 1 validates field 1 AND returns true or False based On Validation
 k = function 2 validates field 2 AND  returns true or False based On Validation
 k = function 3 validates field 3 AND returns true or False based On Validation
 return k;
 //At The End Of This Function You Need To Get "TRUE" To Truly Submit.
}

On Submit :

If(Validation() == true)
{
then Only Submit
}

Write Back If It Dosen't Works...

答案 1 :(得分:0)

<html>
    <body>
    <div id="popup">
        <textarea cols="30" rows="2" name="required" id="required"></textarea>
        <input type="submit" id="click" value="Submit">
               <input type="reset" value="Reset" />
    </div>
  <script>
    var click = document.getElementById("click");
click.addEventListener("click", function() {
  var required = document.getElementById("required").value;
  if (required===null || required==="") {
    alert("Please make sure all required field are completed");
  }
  else  {
    alert("Thank you! \nYour sumbission has been accepted and you will receive a conformation email shortly! \nYou will now be taken to the Home page.");
    window.location.replace("http://stackoverflow.com", 5000);
  }
});
    </script>
    </body>
    </html>

http://jsbin.com/wuxahape/1/