onSubmit event on a button element will it work? [closed]

若如初见. 提交于 2020-08-20 08:54:19

问题


The following is the code.

<html>
    <head>
        <script type="text/javascript">
            function call() {
                alert("hai");
            }
        </script>
    </head>
    <body>
        <form name="login" method="post" action="">
            <input type="submit" name="Submit" value="login" onSubmit="call()">
        </form>
    </body>
</html>

Is it possible to create an event onSubmit inside the input element type button.

This is for an assignment. I was told by my tutor that it is possible. Any help would be highly appreciated.

Thank you.


回答1:


Your onsubmit should be applied to the form as it's the form that gets submitted - not the button.

From the HTML4 specification:

The onsubmit event occurs when a form is submitted. It only applies to the FORM element.




回答2:


Actually "onsubmit" is an event of form element. If you want to catch form submit event (which can be done by clicking button, automatically or by pressing enter, you need to do:

<form ... onsubmit="func();">

otherwise you can catch only submit button click event like:

<input type="submit" ... onclick="func();">



回答3:


I think this is not possible.

You should hook into onclick event of submit. A Form can have onsubmit event but a button cannot have the onsubmit.

You should do like this:

<input type="submit" name="Submit" value="login" onclick="call();">


来源:https://stackoverflow.com/questions/15410128/onsubmit-event-on-a-button-element-will-it-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!