开发者

How to get the right target when submitting a form in jQuery?

开发者 https://www.devze.com 2022-12-17 04:25 出处:网络
$(\'#form\').submit(function(event) { }); When user submits the form by click <input type=\"submit\" />,it should be <input type=\"submit\" />,
$('#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...

0

精彩评论

暂无评论...
验证码 换一张
取 消