javascript代码无法运行。似乎无法在代码中找到任何错误。必须是一些小错误。我在这里检查过osts的逻辑。
<html>
<head>
<title></title>
<script>
function finddays()
{
var today = new Date();
var user = new Date(myform.year.value,myform.month.value,myform.day.value,0,0,0,0);
var msPerDay = 24 * 60 * 60 * 1000;
var daysLeft = Math.abs((user.getTime() - today.getTime())/msPerDay);
daysLeft = Math.round(daysLeft);
retun(daysLeft);
}
</script>
</head>
<body>
<form name="myform">
<input type="number" name="date" value="date" min="1" max="31">
<input type="number" name="month" value="month" min="1" max="12">
<input type="number" name="year" value="year" min="1980" max="2200">
<input type="submit" name="submit" value="SUBMIT" onclick="windows.alert ('Number of days: '+ finddays())">
</form>
</body>
</html>
答案 0 :(得分:1)
@Riya Tilwani
您缺少以下内容:
new Date(myform.year.value,myform.month.value,myform.date.value,0,0,0,0)
return(daysLeft);
window.alert
var user = new Date(myform.year.value,myform.month.value-1,myform.date.value,0,0,0,0);我已经测试了代码,如果您进行了上述更改,则可以正常运行。
答案 1 :(得分:0)
这样可以,只需复制粘贴完整的代码。
<html>
<head>
<title></title>
<script>
function finddays()
{
var today = new Date();
var user = new Date(myform.year.value,myform.month.value,myform.date.value,0,0,0,0);
var msPerDay = 24 * 60 * 60 * 1000;
var daysLeft = Math.abs((user.getTime() - today.getTime())/msPerDay);
daysLeft = Math.round(daysLeft);
alert('Number of days: '+daysLeft);
}
</script>
</head>
<body>
<form name="myform">
<input type="number" name="date" value="date" min="1" max="31">
<input type="number" name="month" value="month" min="1" max="12">
<input type="number" name="year" value="year" min="1980" max="2200">
<input type="submit" name="submit" value="SUBMIT" onclick="finddays();">
</form>
</body>
</html>
答案 2 :(得分:0)
你犯了两个错误:
1)使用myform
而不声明
2)使用提交按钮代替按钮。
请将您的代码更改为:
HTML :
<form name="myform" id="myform">
<input type="number" name="date" value="date" min="1" max="31"/>
<input type="number" name="month" value="month" min="1" max="12"/>
<input type="number" name="year" value="year" min="1980" max="2200"/>
<input type="button" name="submit" value="SUBMIT" onclick="finddays();"/>
</form>
Javascript :
function finddays()
{
var myform=document.getElementById("myform");
var today = new Date();
var user = new Date(myform.year.value,myform.month.value,myform.date.value,0,0,0,0);
var msPerDay = 24 * 60 * 60 * 1000;
var daysLeft = Math.abs((user.getTime() - today.getTime())/msPerDay);
daysLeft = Math.round(daysLeft);
alert('Number of days: '+daysLeft);
}
<强> Check out JSFiddle 强>