最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

form onsubmit inline javascript - Stack Overflow

programmeradmin4浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 8

In 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;">
发布评论

评论列表(0)

  1. 暂无评论