【问题标题】:Display second prompt if the first prompt is not TRUE如果第一个提示不是 TRUE,则显示第二个提示
【发布时间】:2022-02-12 16:22:08
【问题描述】:

当我尝试显示新提示时遇到问题,我使用循环,但它会同时显示我的提示。这是我的代码,我真的需要帮助

<body>
<p id="age" style="font-weight: bold"></p>
<button onclick=" notify()">Try it</button>
<script>
function notify(){
var age = document.getElementById("age").value;
var age = prompt("Enter Your birth year:");// This is the first prompt 
const year = new Date().getFullYear();
if (age != null) { 
          document.getElementById("age").innerHTML =
            "Your " + age + " is so beautiful"; // If user enter birth year < current year will display this
        }
  do {
     age = prompt("Re-enter your birth year:"); // Ortherwise, this will display and they need to enter until birth year < current year
    } while (age > year && age != null);
        var tuoi = year - age;// This is just calculate user'age, for example if the user enter 2000 will display user'age is 22
        document.getElementById("age").innerHTML =
          "Tuổi " + tuoi + " này đẹp đó";
}
</script>
</body>

【问题讨论】:

    标签: javascript html css


    【解决方案1】:

    您的代码有几个问题,并且做了一些对我来说完全没有意义的事情,但我认为您想要这样的事情:

    <body>
      <p id="age" style="font-weight: bold"></p>
      <button onclick=" notify()">Try it</button>
      <script>
        function notify() {
          const year = new Date().getFullYear();
          var birth_year = parseInt(prompt("Enter Your birth year:")); // This is the first prompt
          
          if (!isNaN(birth_year) && birth_year < year) {
            document.getElementById("age").innerHTML =
              "Your " + birth_year + " is so beautiful"; // If user enter birth year < current year will display this
          } else {
            do {
              birth_year = parseInt(prompt("Enter Your birth year:")); // Ortherwise, this will display and they need to enter until birth year < current year
            } while (isNaN(parseInt(birth_year)) || birth_year >= year);
            var tuoi = year - age; // This is just calculate user'age, for example if the user enter 2000 will display user'age is 22
            document.getElementById("age").innerHTML =
              "Tuổi " + birth_year + " này đẹp đó";
          }
        }
    
      </script>
    </body>
    

    【讨论】:

      【解决方案2】:

      你只是忘记了你的 if 条件的一部分。

      if (age != null) 更改为if (age != null &amp;&amp; age &lt; year),它将按您的预期工作。如果年龄 == 年,请不要忘记大小写。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-04-15
        • 2014-12-18
        • 2020-12-12
        • 2017-08-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多