问题
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