简体   繁体   English

为什么不显示第二个警报

[英]Why does a second alert not show up

I have a little program that checks if a email address contains the right elements, but it doesn't show the box that gives conformation.我有一个小程序可以检查电子邮件地址是否包含正确的元素,但它没有显示提供确认的框。

 $(document).ready(function(){ $("#sendButton").click(function(){ var name = $("#name").val(); var email = $("#email").val(); if (email.indexOf('@') === -1) { alert("Please enter a valid email."); return; } if ((email.indexOf(".com") === -1)) { alert("Please enter a valid email."); return; } alert(`Thank you ${name}, There has been sent a conformation to ${email}.`); }); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <p><label for="name">Name:</label><input id="name"></p> <p><label for="email">Email:</label><input id="email"></p> <p><button type="button" id="sendButton">Send</button></p>

Is there someone who knows how to show the "Thank you" alert box?有没有人知道如何显示“谢谢”提醒框?

The posted code works just fine and looks good to me.发布的代码工作得很好,对我来说看起来不错。 I think you might be using an old browser (ie probably Internet Explorer) which doesn't support template strings (these ${variableName} symbols inside your message) and multiline strings.我认为您可能正在使用不支持模板字符串(消息中的这些${variableName}符号)和多行字符串的旧浏览器(即可能是 Internet Explorer)。 You can change the last alert to something like this:您可以将最后一个alert更改为如下所示:

alert("Thank you " + name + ", There has been sent a conformation to  " + email + ".");

It's basically equal to your existing code, but is compliant with the older version of the standard.它基本上等同于您现有的代码,但符合旧版本的标准。

我通过删除 if 语句中的 return 语句解决了这个问题,并将这个 return 语句放在函数的末尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM