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;
};
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745359458a4624293.html
评论列表(0条)