我需要幫助修復我的html和java腳本程式碼
P粉201448898
P粉201448898 2023-08-01 20:25:03
0
1
598
<p>我想做一個地方,在那裡輸入你的下一個生日,頁面就會變成一個視窗。提醒你還有幾天就是你的生日了。當我運行代碼時,在我輸入生日之前,警報顯示NaN,這意味著不是一個數字。我想讓它工作後,我鍵入生日後,我點擊提交。這是我寫的程式碼:</p> <p>` </p> <pre class="brush:php;toolbar:false;"><input type="submit" value="Submit"> </form> <script> 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); </script> </body>`</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>
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板