javascript - Submit form and observe the onsubmit event - Stack Overflow

This should be trivial, but I am not getting it :HTML<form id="myform" action="#&quo

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

2 Answers 2

Reset to default 2

Change 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;
};

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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信