如果沒有輸入用戶名,年齡提示仍然出現,如果沒有輸入用戶名,我希望整個提示終止,基本上,當它說“你不能沒有名字就繼續”時,提示要求輸入年齡仍然出現,我不希望發生這種情況。
這是我的代碼:
</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";
}
}
uj5u.com熱心網友回復:
您只需要return
在 if 陳述句中的警報之前插入一個相對于缺少的用戶名案例的警報:
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>
uj5u.com熱心網友回復:
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>
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/487425.html