我需要帮助修复我的html和java脚本代码
P粉201448898
P粉201448898 2023-08-01 20:25:03
[HTML讨论组]
<p>我想做一个地方,在那里输入你的下一个生日,页面就会变成一个窗口。提醒你还有几天就是你的生日了。当我运行代码时,在我输入生日之前,警报显示NaN,这意味着不是一个数字。我想让它工作后,我键入生日后,我点击提交。这是我编写的代码:</p> <p>` </p> <pre class="brush:php;toolbar:false;">&lt;input type="submit" value="Submit"&gt; &lt;/form&gt; &lt;script&gt; let date_1 = new Date(document.getElementById("bday").value); let date_2 = new Date(); let difference = date_1.getTime() - date_2.getTime(); let TotalDays = Math.ceil(difference / (1000 * 3600 * 24)); window.alert(TotalDays); &lt;/script&gt; &lt;/body&gt;`</pre> <p><br /></p>
P粉201448898
P粉201448898

全部回复(1)
P粉523625080

<!DOCTYPE html>
<html>
<head>
  <title>Birthday Countdown</title>
</head>
<body>
  <form onsubmit="calculateDaysLeft(event)">
    <label for="bday">Enter your birthday:</label>
    <input type="date" id="bday" name="bday" required>
    <input type="submit" value="Submit">
  </form>

  <script>
    function calculateDaysLeft(event) {
      event.preventDefault(); // Prevent form submission to avoid page reload

      // Get the user's birthday from the input field
      let userBirthday = new Date(document.getElementById("bday").value);

      // Get the current date
      let currentDate = new Date();

      // Calculate the difference in milliseconds
      let difference = userBirthday.getTime() - currentDate.getTime();

      // Calculate the difference in days and show the alert
      let totalDays = Math.ceil(difference / (1000 * 3600 * 24));
      window.alert(`There are ${totalDays} days left until your birthday!`);
    }
  </script>
</body>
</html>
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号