我需要帮助修复我的HTML和JavaScript代码。

huangapple go评论62阅读模式
英文:

I need help fixing my html and java script code

问题

我想创建一个地方,你可以输入你的下一个生日,页面会使用 window.alert 告诉你距离你的生日还有多少天。当我运行这段代码时,在我输入生日之前,警报显示 NaN,这意味着不是一个数字。我希望在我输入生日并点击提交后它能够正常工作。这是我的代码:

<form name="form">
    <input type="date" id="bday"><br><br>

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

注意: 这段代码在加载页面时就会计算日期差异并显示 window.alert,而不是在点击提交按钮后再进行计算。如果你想在点击提交按钮后才计算,需要将 JavaScript 代码放入一个函数中,并在提交按钮的点击事件上调用该函数。

英文:

I want to make a place where you key in your next birthday and the page does a window.alert to tell you how many days are left for your birthday. When I run the code, before I key in the birthday, the alert says NaN which means not a number. I want it to work after I key in the birthday after I click submit. This is what I coded:

` <form name = "form">
<input type="date" id="bday"><br><br>

&lt;input type=&quot;submit&quot; value=&quot;Submit&quot;&gt;
    
&lt;/form&gt;

&lt;script&gt;
let date_1 = new Date(document.getElementById(&quot;bday&quot;).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;`

答案1

得分: 0

我看到基本上有两个问题。
首先,你从bday获取的值将是一个字符串,所以你应该确定该值的格式。你需要获取日、月和年,以创建一个字符串,例如"day/month/year",然后使用Date.parse()将其转换为日期类型。

其次,要获取实际日期,你应该使用Date.now()。仅构造函数本身不会起作用。

应用这些更改后,我们有:

let date_1 = Date.parse("13/10/2023");
let date_2 = Date.now();

let difference = date_1 - date_2;
let TotalDays = Math.ceil(difference / (1000 * 3600 * 24));
window.alert(TotalDays);
英文:

I see basically two problems here.
First of all, the value that you are getting from bday is going to be a string, so you should be sure of the format of that value. You need to get the day, the month and the year, in order to create a string like "day/month/year", to convert it into a date type, using Date.parse().

Secondly, to get the actual date, you should use Date.now(). The constructor alone will not work.

Apllying these changes we have:

let date_1 = Date.parse(&quot;13/10/2023&quot;);
let date_2 = Date.now();

let difference = date_1 - date_2;
let TotalDays = Math.ceil(difference / (1000 * 3600 * 24));
window.alert(TotalDays);

答案2

得分: -1

&lt;!-- 开始代码段:js 隐藏:false 控制台:true Babel:false --&gt;

&lt;!-- 语言:lang-js --&gt;

    &lt;!DOCTYPE html&gt;
    &lt;html&gt;
    &lt;head&gt;
      &lt;title&gt;生日倒计时&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
      &lt;form onsubmit=&quot;calculateDaysLeft(event)&quot;&gt;
        &lt;label for=&quot;bday&quot;&gt;输入您的生日:&lt;/label&gt;
        &lt;input type=&quot;date&quot; id=&quot;bday&quot; name=&quot;bday&quot; required&gt;
        &lt;input type=&quot;submit&quot; value=&quot;提交&quot;&gt;
      &lt;/form&gt;

      &lt;script&gt;
        function calculateDaysLeft(event) {
          event.preventDefault(); // 防止表单提交以避免页面重新加载

          // 从输入字段获取用户生日
          let userBirthday = new Date(document.getElementById(&quot;bday&quot;).value);

          // 获取当前日期
          let currentDate = new Date();

          // 计算毫秒差异
          let difference = userBirthday.getTime() - currentDate.getTime();

          // 计算天数差异并显示警报
          let totalDays = Math.ceil(difference / (1000 * 3600 * 24));
          window.alert(`距离您的生日还有 ${totalDays} 天!`);
        }
      &lt;/script&gt;
    &lt;/body&gt;
    &lt;/html&gt;

&lt;!-- 结束代码段 --&gt;
英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

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

  &lt;script&gt;
    function calculateDaysLeft(event) {
      event.preventDefault(); // Prevent form submission to avoid page reload

      // Get the user&#39;s birthday from the input field
      let userBirthday = new Date(document.getElementById(&quot;bday&quot;).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!`);
    }
  &lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月31日 21:09:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76803962.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定