![](/img/trans.png)
[英]How do I stop the text from appearing then disappearing a second later, using Javascript?
[英]How do I stop the second prompt from appearing until the first if/else condition is satisfied? (Start Page for a game)
如果沒有輸入用戶名,年齡提示仍然出現,如果沒有輸入用戶名,我希望整個提示終止,基本上,當它說“沒有名字你不能繼續”之后提示要求年齡仍然出現,我不希望這種情況發生。
這是我的代碼:
</style>
<body>
<h1>Welcome to </h1>
<h2> Hangman </h2>
<div class="wrapper">
<button onclick="myFunction()">Let's start</button>
</div>
<script>
function myFunction() {
let person = prompt("Please enter your name", "Marium");
if (person == null || person == "") {
alert("You cannot proceed without entering the name");
}
else {
alert("Hello " + person);
}
let age= prompt("Please enter your age");
if (age<16) {
alert("User below 16 cannot proceed");
}
else {
window.location.href= "sample.html";
}
}
您只需要在 if 語句中的警報之前插入一個與缺少用戶名案例相關的return
值:
function myFunction() { let person = prompt("Please enter your name", "Marium"); if (person == null || person == "") { return alert("You cannot proceed without entering the name"); } else { alert("Hello " + person); } let age = prompt("Please enter your age"); if (age < 16) { alert("User below 16 cannot proceed"); } else { window.location.href = "sample.html"; } }
<h1>Welcome to </h1> <h2> Hangman </h2> <div class="wrapper"> <button onclick="myFunction()">Let's start</button> </div>
添加return;
后
alert("You cannot proceed without entering the name");
代碼:
<body>
<h1>Welcome to </h1>
<h2> Hangman </h2>
<div class="wrapper">
<button onclick="myFunction()">Let's start</button>
</div>
<script>
function myFunction() {
let person = prompt("Please enter your name", "Marium");
if (person == null || person == "") {
alert("You cannot proceed without entering the name");
return;
}
else {
alert("Hello " + person);
}
let age= prompt("Please enter your age");
if (age<16) {
alert("User below 16 cannot proceed");
}
else {
window.location.href= "sample.html";
}
}
</style>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.