JavaScript计算时差函数不起作用

时间:2017-09-02 17:40:32

标签: javascript function

当我按下“Check for Due”按钮时,它不会将我重定向到alert()函数。有任何修复错误的建议吗?

QtWebView

当我按下“Check for Due”按钮时,它不会将我重定向到alert()函数。有任何修复错误的建议吗?

1 个答案:

答案 0 :(得分:0)

您的提示文字不正确。它应该是"overdue by: " + z + "days"。 此外,您可以使用template string

使用模板字符串,您的字符串消息应为overdue by: ${z} days

function Fine(){
  var currentDate = new Date(document.getElementById("currentDate").value);
  var dueDate = new Date(document.getElementById("dueDate").value);
  var z = parseInt((currentDate - dueDate) / (24 * 3600 * 1000));

  alert(`overdue by:  ${z} days`);
}
<form name = "myForm" method = "POST" action = "form.html">
  <table>
    <tr>
      <td> Book Returning Due Date:  </td>
      <td><input type = "date" name = "due_date" placeholder = "MM/DD/YYYY" id = "dueDate" /></td>
    </tr>

    <tr>
      <td> Current Date:  </td>
      <td><input type = "date" name = "current_date" placeholder = "MM/DD/YYYY" id = "currentDate" /></td>
    </tr>

    <tr>
      <td>
        <input class = "button-1" type="button" name = "due_fine" value = "Check for Due" onclick = "Fine();" /> &nbsp; &nbsp;
        <input class = "button-2" type = "reset" value = "Reset">
      </td>
    </tr>
  </table>
</form>