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').
精彩评论