admin管理员组文章数量:1422007
This should be trivial, but I am not getting it :\
HTML
<form id="myform" action="#">
<input type="submit" value="Submit" />
<a href="#" onclick="$('myform').submit()">Submit</a>
</form>
JS (Prototype)
$('myform').observe('submit', function(e) {
alert('submitted!');
e.stop();
});
Why the submit is only triggered with the submit via input? Shouldn't the submit event be triggered with $('myform').submit()
also?
JSFiddle: /
This should be trivial, but I am not getting it :\
HTML
<form id="myform" action="#">
<input type="submit" value="Submit" />
<a href="#" onclick="$('myform').submit()">Submit</a>
</form>
JS (Prototype)
$('myform').observe('submit', function(e) {
alert('submitted!');
e.stop();
});
Why the submit is only triggered with the submit via input? Shouldn't the submit event be triggered with $('myform').submit()
also?
JSFiddle: http://jsfiddle/d8BdM/
Share Improve this question edited Jul 19, 2019 at 17:25 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Mar 5, 2013 at 10:50 Rui CarneiroRui Carneiro 5,6715 gold badges35 silver badges39 bronze badges 1- 1 When a form is submitted by JavaScript the onsubmit event handler is never executed: quirksmode/js/forms.html#methods – Victor Commented Mar 7, 2013 at 11:06
2 Answers
Reset to default 2Change the form like this:
<form id="myform" action="">
<input type="submit" value="Submit" id="sub_but" />
<a href="#" onclick="$('sub_but').click();">Submit</a>
</form>
And keep the other code; i tested in your fiddle and works.
Saludos ;)
Change your javascript code like this:
$('myform').onsubmit = function() {
alert('submitted!');
return false;
};
本文标签: javascriptSubmit form and observe the onsubmit eventStack Overflow
版权声明:本文标题:javascript - Submit form and observe the onsubmit event - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745359457a2655228.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论