admin管理员组文章数量:1355570
I am trying to put an inline onsubmit script on a form, but it's not working:
<!DOCTYPE html>
<html>
<head></head>
<body>
<form onsubmit="(function(event){console.log(event); return false;})(this);">
<button type="submit">Submit</button>
</form>
</body>
</html>
And is the parameter for the function the event or the form element?
I am trying to put an inline onsubmit script on a form, but it's not working:
<!DOCTYPE html>
<html>
<head></head>
<body>
<form onsubmit="(function(event){console.log(event); return false;})(this);">
<button type="submit">Submit</button>
</form>
</body>
</html>
And is the parameter for the function the event or the form element?
Share Improve this question asked Feb 22, 2016 at 23:01 poashoaspoashoas 1,8942 gold badges25 silver badges47 bronze badges 1-
In the inline JS,
this
is the form, not the event. – Barmar Commented Feb 22, 2016 at 23:02
1 Answer
Reset to default 8In inline Javascript, this
is always the element itself. event
is available as a variable local to the inline Javascript, so you can write:
<form onsubmit="return (function(event){console.log(event); return false;})(event);">
Since you're using an IIFE, its return
statement returns from the function, but not from the event handler. You need to return what the IIFE returns.
I'm not sure why you're using an IIFE, you can just write:
<form onsubmit="console.log(event); return false;">
本文标签: form onsubmit inline javascriptStack Overflow
版权声明:本文标题:form onsubmit inline javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743963883a2569531.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论