18910140161

HTML-当试图使用JavaScript获取用户输入时收到“无效日期”-堆栈溢出

顺晟科技

2022-10-19 12:34:36

194

所以,我试图用JavaScript计算用户输入的两个日期之间的差异;但是,我不断收到invaliddate。如有任何帮助,不胜感激。

以下是我的代码:

<!DOCTYPE html>
<html>
<head>
    <title>Assignment Calculator</title>
</head>
<body>

<h1>Assignment Calculator</h1>
    
<form>
    <label for="StartDate">Date to Start:</label>
    <input id="StartDate" name="StartDate" type="date"/>

    <label for="DueDate">Due Date:</label> 
    <input id="DueDate" name="DueDate" type="date"/>
    
    <input type="submit" value="Submit" onclick="displayDates()">
</form>
    
<script type = "text/javascript" >

// Get start date and due date 

var startInput = document.getElementById("StartDate").value;
var dueInput = document.getElementById("DueDate").value;
    
var startDate = new Date(startInput);
var dueDate = new Date(dueInput);

// To calculate the time difference of two dates
var DifferenceInTime = dueDate.getTime() - startDate.getTime();

// To calculate the no. of days between two dates
var DifferenceInDays = DifferenceInTime / (1000 * 3600 * 24);

function displayDates() {
//To display the of days a student has to work on the assignment (result)
document.write("Start your assignment by " + startDate + ".<br>"
            + "Finish your assignment by " + dueDate + ".<br> "
            + "You must complete your assignment in this many days: " + DifferenceInDays + ".");
}
</script>

</body>
</html> 

顺晟科技:

您调用按钮单击上的函数,但即使没有选择数据值,也会在pageload上从on input字段获取值。 您也应该将该部分放在您在单击按钮时调用的函数中。

<!DOCTYPE html>
<html>
<head>
    <title>Assignment Calculator</title>
</head>
<body>

<h1>Assignment Calculator</h1>
    
<form>
    <label for="StartDate">Date to Start:</label>
    <input id="StartDate" name="StartDate" type="date"/>

    <label for="DueDate">Due Date:</label> 
    <input id="DueDate" name="DueDate" type="date"/>
    
    <input type="submit" value="Submit" onclick="displayDates()">
</form>
    
<script type = "text/javascript" >

// Get start date and due date 

var startInput = document.getElementById("StartDate").value;
var dueInput = document.getElementById("DueDate").value;
    
var startDate = new Date(startInput);
var dueDate = new Date(dueInput);

// To calculate the time difference of two dates
var DifferenceInTime = dueDate.getTime() - startDate.getTime();

// To calculate the no. of days between two dates
var DifferenceInDays = DifferenceInTime / (1000 * 3600 * 24);

function displayDates() {
//To display the of days a student has to work on the assignment (result)
document.write("Start your assignment by " + startDate + ".<br>"
            + "Finish your assignment by " + dueDate + ".<br> "
            + "You must complete your assignment in this many days: " + DifferenceInDays + ".");
}
</script>

</body>
</html> 

只需将计算日期差异的代码移到函数中。

将函数外部的所有JavaScript代码放入函数中。

<!DOCTYPE html>
<html>
<head>
    <title>Assignment Calculator</title>
</head>
<body>

<h1>Assignment Calculator</h1>
    
<form>
    <label for="StartDate">Date to Start:</label>
    <input id="StartDate" name="StartDate" type="date"/>

    <label for="DueDate">Due Date:</label> 
    <input id="DueDate" name="DueDate" type="date"/>
    
    <input type="submit" value="Submit" onclick="displayDates()">
</form>
    
<script type = "text/javascript" >

// Get start date and due date 

var startInput = document.getElementById("StartDate").value;
var dueInput = document.getElementById("DueDate").value;
    
var startDate = new Date(startInput);
var dueDate = new Date(dueInput);

// To calculate the time difference of two dates
var DifferenceInTime = dueDate.getTime() - startDate.getTime();

// To calculate the no. of days between two dates
var DifferenceInDays = DifferenceInTime / (1000 * 3600 * 24);

function displayDates() {
//To display the of days a student has to work on the assignment (result)
document.write("Start your assignment by " + startDate + ".<br>"
            + "Finish your assignment by " + dueDate + ".<br> "
            + "You must complete your assignment in this many days: " + DifferenceInDays + ".");
}
</script>

</body>
</html> 

几乎所有的javsascript代码都需要函数内部移动。

当它在代码之外时,它会立即运行,因此像and这样的变量将被设置一次,以后再也不会设置了。

您的目标是在用户单击按钮后找出并

  • TAG:
相关文章
我们已经准备好了,你呢?
2024我们与您携手共赢,为您的企业形象保驾护航