html输入onchange不接受匿名函数

doo*_*son 5 html javascript model-view-controller input

为什么这不起作用?

 <input type="file" id="kmlFiles2" multiple onchange="function(){alert('why does this not work')}()">
Run Code Online (Sandbox Code Playgroud)

chrome给我一个错误, Uncaught SyntaxError:Unexpected token(.

Firefox告诉我SyntaxError:function语句需要一个名字.

但这确实有效吗?

 <input type="file" id="kmlFiles2" multiple onchange="alert('but this does work')">
Run Code Online (Sandbox Code Playgroud)

http://jsfiddle.net/ewzyV/

我问,因为我正在尝试使用和MVC框架,将代码注入onchange事件.

alj*_*gom 11

onchange="(function(){alert('this should work')})()"
Run Code Online (Sandbox Code Playgroud)

  • 错误,nm,这似乎工作`onchange ="(function(e){e.preventDefault(); alert('this should working');})(event)"` (2认同)