Javascript警报窗口 - 如何设置len

时间:2012-04-08 09:24:33

标签: javascript window alert

我有一个HostGator提供的基本HTML / JavaScript联系表单。它工作得很好......如果用户没有提交任何名称或无效的电子邮件地址,则会弹出一个JavaScript警告弹出窗口。这些警报会保留在屏幕上,直到用户单击“确定”但是,如果用户正确提交联系表格,则最终成功提醒仅持续1/2秒左右。我正在尝试弄清楚如何在指定的时间内完成最后的电子邮件成功提醒,或者直到用户点击“确定”按钮,然后将其重定向到指定的页面。以下是联系表单上带有JavaScript http://www.hostgator.com/formmail.shtml的HostGator信息的链接,这里是我试图更改以获取提交后的JavaScript消息的代码,而不是在用户甚至有消息之前消失有机会阅读它。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>FormMail Demo</title>
<script type="text/javascript">
function hgsubmit()
{
if (/\S+/.test(document.hgmailer.name.value) == false) alert ("Please provide your name.");
else if (/^\S+@[a-z0-9_.-]+\.[a-z]{2,6}$/i.test(document.hgmailer.email.value) == false) alert ("A valid email address is required.");
 else if (/\S+/.test(document.hgmailer.comment.value) == false) alert ("Your email content is needed.");
  else {
       document.hgmailer.submit();
       alert ('Thank you!\nYour email is sent.');
       }
}
</script>
</head>
<body>
<form action="http://www.mydomain.com/cgi-sys/formmail.pl" method="post" name="hgmailer">
<input type="hidden" name="recipient" value="myemail@mydomain.com">
<input type="hidden" name="subject" value="FormMail E-Mail">
Whatever you want to say here<br><br>
Visitor Name: <input type="text" name="name" size="30" value=""><br>
Visitor E-Mail: <input type="text" name="email" size="30" value=""><br>
E-Mail Content: <textarea name="comment" cols="50" rows="5"></textarea><br><br>
<input type="button" value="E-Mail Me!" onclick="hgsubmit();">
<input type="hidden" name="redirect" value="http://www.mydomain.com/redirect-path">
</form>
</body>
</html>

3 个答案:

答案 0 :(得分:0)

这里的问题似乎是在document.hgmailer.submit();仍在进行过程中显示弹出窗口。由于提交延迟,您会在submit()完成之前看到弹出窗口一小段时间,然后您将被重定向到目标页面。

一种方法是将alert(....)放在alert(....)之前,或者您可以使用jQuery异步发布数据,如:

....submit()

编辑:

或者你可以避免这整个弹出式业务并执行此操作:

....submit()

也就是说,您基本上可以将 $.post("target_Page_Address", { var1: value1, var2: value2 // and so on }, function(response){ // Do something based on response } ); 的提交按钮设置为 document.hgmailer.submit_button_name.value= 'Please wait...'; document.hgmailer.submit();

value 通过这种方式,用户将知道他们的电子邮件仍在处理中,并且在完成之前不会更改页面。

答案 1 :(得分:0)

   else {
     alert('Thank you!\nYour email is sent.');
     setTimeout("document.hgmailer.submit()", 4*1000);
   }

这应该做alert(),等待4秒(或你在第二个参数中指定的任何内容),然后执行submit()。

答案 2 :(得分:0)

您可以轻松停止表单提交,直到用户点击上次提醒的“确定”按钮。 使用

alert ('Thank you!\nYour email is sent.');
//waits here  until user click 'ok', then submit!
document.hgmailer.submit();

而不是

document.hgmailer.submit();
alert ('Thank you!\nYour email is sent.');