Javascript "Expect ;"int 上

标签 javascript asp.net-mvc-4 debugging

我不断收到“预期;”我定义的三个 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/

相关文章:

javascript - "Member not found"- jQuery 无法在 MSIE 中工作,但可以在 Chrome 中工作 -

javascript - 在页面加载期间使用 firecookie 捕获 cookie

r - 从 do.call 内部调用时,R/Rstudio 中的调试浏览器崩溃

: (e && e. 目标的 JavaScript 代码说明) || (window.event && window.event.srcElement)

javascript - 使soundmanager插件声音静音

javascript - JQuery:添加类后淡出?

r - 在 R 调试器中显示上下文?

javascript - 错误 : Could not find source map for in Angular2, Karma、Webpack 和 Istanbul 尔

asp.net-mvc-4 - MVC 项目中的 Azure Redis 缓存

asp.net-mvc-4 - 如何在 ASP.NET MVC 4 上使用 SimpleMembership 启用角色管理器