html 在选择选项时多次提示用户

oknwwptz  于 2023-03-27  发布在  其他
关注(0)|答案(1)|浏览(142)

我很难做到这一点:
1.如视频中所示,创建一个带有脚本标记的新HTML文件。提示用户以数字形式输入一周中的某一天,其中1是星期日,7是星期六。根据用户的数字输入,使用switch... case语句确定日期并将结果输出到控制台。例如,如果用户输入6,则输出应为“Friday”。
1.向用户显示水果和价格的菜单,类似于下面的示例:

1) Apple $.99
 2) Pear $.79
 3) Orange $1.19
 4) Grapes $1.49
 5) Grapefruit $1.79
 6) Kiwi $.79
 7) Apricots $1.29

1.提示用户两次。第一次,提示用户他们想要购买的水果(1-7)。接下来提示用户他们想要购买的水果数量。将这些值记录到变量中,然后将用户的订单输出到屏幕上,如下所示:

10 Apples = $9.90
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Switch Case Statment</title>
</head>
<body>
    <div>
        <h2>Choose a fruit</h2>
        <p>1. Apple $0.99.<br/>2. Pear $0.79.<br/>3. Orange $1.19.<br/> 4. Grapes $1.49.<br/>5. Grape Fruit $1.79.<br/> 6. Kiwi $0.79.<br/> 7. Apricots $1.29.</p>
    </div>
    <div id="output"></div>
    <script>
        //Days of the Week Start
        let daysOfWeek = prompt("Enter a numerical value for days of the week, where 1 is Sunday [1-7]");

        switch(daysOfWeek){
            case "1":
                console.log("Sunday");
                break;
            case "2":
                console.log("Monday");
                break;
            case "3":
                console.log("Tuesday");
                break;
            case "4":
                console.log("Wednesday");
                break;
            case "5":
                console.log("Thursday");
                break;
            case "6":
                console.log("Friday");
                break;
            case "7":
                console.log("Saturday");
        }
        if(daysOfWeek <=7){
            let fruit = prompt ("Choose a fruit [1-7]");
        }
        
        //Days of the week end.
        
    </script>
</body>
</html>

我已经设法做到这一点,但第二个提示是隐藏我的html,所以用户不能看到水果列表。
你能帮我解决这个问题吗?谢谢奥斯瓦尔德。

eulz3vhy

eulz3vhy1#

通过执行window.onload事件处理程序中的代码等待页面完成加载....我认为这会起作用,但对我来说不起作用。所以我不得不用setTimeout添加延迟?

window.onload = () => {
  setTimeout(() => {
    //Days of the Week Start
    let daysOfWeek = prompt("Enter a numerical value for days of the week, where 1 is Sunday [1-7]");

    switch (daysOfWeek) {
      case "1":
        console.log("Sunday");
        break;
      case "2":
        console.log("Monday");
        break;
      case "3":
        console.log("Tuesday");
        break;
      case "4":
        console.log("Wednesday");
        break;
      case "5":
        console.log("Thursday");
        break;
      case "6":
        console.log("Friday");
        break;
      case "7":
        console.log("Saturday");
    }
    if (daysOfWeek <= 7) {
      let fruit = prompt("Choose a fruit [1-7]");
    }

    //Days of the week end.
  }, 100)
 };
<div>
  <h2>Choose a fruit</h2>
  <p>1. Apple $0.99.<br/>2. Pear $0.79.<br/>3. Orange $1.19.<br/> 4. Grapes $1.49.<br/>5. Grape Fruit $1.79.<br/> 6. Kiwi $0.79.<br/> 7. Apricots $1.29.</p>
</div>
<div id="output"></div>

相关问题