简体   繁体   中英

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. You can change the last alert to something like this:

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 语句放在函数的末尾。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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