开发者

How to code a general js function for all form submit events?

I want to code a single js/jquery function for all forms submit events in my application.

Currently I am hardcoding on 3 locations like that and it is working but I have to create a functi开发者_开发知识库on for each form separately:

jQuery('#myForm').live('submit',function(event) { // #myForm hardcoded here
    $.ajax({
        url: 'one.php', // one.php hardcoded here
        type: 'POST',
        data: $('#myForm').serialize(), // #myForm hardcoded here
        success: function( data ) {
            alert(data);
        }
    });
    return false;
});

Thanks


jQuery('form').live('submit',function(event) {
    $.ajax({
        url: $(this).attr('action'),
        type: 'POST',
        data: $(this).serialize(),
        success: function( data ) {
            alert(data);
        }
    });
    return false;
});


  • You can probably replace $('#myForm') by $('form') or something else which matches all forms.
  • one.php is probably the action attribute on the form, or $('form').attr('action').
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜