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

本文标签: javascriptSubmit form and observe the onsubmit eventStack Overflow