How to get the right target when submitting a form in jQuery?
$('#form').submit(function(event) {
});
When user submits the form by click <input type="submit" />
,it should be <input type="submit" />
,
when user submits the form by pressing Enter
in a <input />
,it should be that <input />
I tr开发者_JS百科ied event.target
,but it's wrong.
This seems to do the trick:
$(document).ready(function() {
var target = null;
$('#form :input').focus(function() {
target = this;
alert(target);
});
$('#form').submit(function() {
alert(target);
});
});
When pressing enter in a filed or by clicking on a <input type="submit" />
it's the <form>
that is submitted.
So the target is the one specified by your your <form>
tag identified by #form
in your example.
I'm not shure what you want to do though...
精彩评论