ホームページ  >  に質問  >  本文

HTML と Java スクリプトのコードを修正するのに助けが必要です

<p>次の誕生日を入力するとページがウィンドウに変わる場所を作りたいです。数日後にあなたの誕生日が来ることを思い出してください。コードを実行すると、誕生日を入力する前に、アラートに NaN (数値ではないことを意味します) と表示されます。自分の誕生日を入力して送信をクリックした後も機能するようにしたいです。これは私が書いたコードです:</p> <p>` </p> <pre class="brush:php;toolbar:false;"><input type="submit" value="Submit"> </フォーム> <スクリプト> let date_1 = new Date(document.getElementById("bday").value); date_2 = 新しい Date() にします。 差分 = date_1.getTime() - date_2.getTime(); とします。 let TotalDays = Math.ceil(差分 / (1000 * 3600 * 24)); window.alert(TotalDays); </スクリプト> </body>`</pre> <p><br /></p>
P粉201448898P粉201448898445日前565

全員に返信(1)返信します

  • P粉523625080

    P粉5236250802023-08-02 11:47:11

    <!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>

    返事
    0
  • キャンセル返事