我不断收到“预期;”我定义的三个 int 变量(月、日、年)出现错误:
<script type="text/javascript">
function validateDateFormat(input)
{
var values = input.value.split("/");
int month = parseInt(values[0]);
int day = parseInt(values[1]);
int year = parseInt(values[2]);
if ((month < 1 || month > 12))
{
alert("Month value: "+ month + " is not a valid month using MM/DD/YYYY format");
input.value = "";
return;
}
if ((String.valueOf(year).length() != 4))
{
alert("Year value: "+ year + " is not a valid year using MM/DD/YYYY format");
input.value = "";
return;
}
if(day < 1 || day > daysInMonth(month, year))
{
alert("Day value: "+ day + " is not a valid day for the month with value: " + month + " using MM/DD/YYYY format");
input.value = "";
return;
}
};
...
当我尝试运行页面时,FireFox 中的调试器会抛出:
语法错误:缺少;之前的声明
我不明白问题出在哪里。即使我只是这样做,int Month = 0 作为第一行,我仍然得到相同的错误。我在 Asp.net MVC 4 上将其与 Razor 一起使用
最佳答案
您不能在 JavaScript 中将变量声明为 int
。
改变
int month = parseInt(values[0]);
至
var month = parseInt(values[0]);
(当然也对以下两行执行此操作)
如果你真的想要类型变量,你可以看看 TypeScript,但我强烈建议你首先深入了解惯用的 JS。
关于Javascript "Expect ;"int 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31119591/