Php 如何为所有表单提交事件编写通用js函数?
我想为应用程序中的所有表单提交事件编写一个js/jquery函数 目前,我正在3个位置上进行硬编码,就像这样,它正在工作,但我必须为每个表单分别创建一个函数:Php 如何为所有表单提交事件编写通用js函数?,php,javascript,jquery,form-submit,Php,Javascript,Jquery,Form Submit,我想为应用程序中的所有表单提交事件编写一个js/jquery函数 目前,我正在3个位置上进行硬编码,就像这样,它正在工作,但我必须为每个表单分别创建一个函数: jQuery('#myForm').live('submit',function(event) { // #myForm hardcoded here $.ajax({ url: 'one.php', // one.php hardcoded here type: 'POST', d
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;
});
谢谢- 您可能可以将$('form')替换为$('form')或其他匹配所有表单的内容
- one.php可能是表单上的action属性,或$('form').attr('action')
jQuery('form').live('submit',function(event) {
$.ajax({
url: $(this).attr('action'),
type: 'POST',
data: $(this).serialize(),
success: function( data ) {
alert(data);
}
});
return false;
});