form onsubmit inline javascript - Stack Overflow

I am trying to put an inline onsubmit script on a form, but it's not working:<!DOCTYPE html>

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;">

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743963883a4537182.html

相关推荐

  • form onsubmit inline javascript - Stack Overflow

    I am trying to put an inline onsubmit script on a form, but it's not working:<!DOCTYPE html>

    2天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信