Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何为所有表单提交事件编写通用js函数?_Php_Javascript_Jquery_Form Submit - Fatal编程技术网

Php 如何为所有表单提交事件编写通用js函数?

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

我想为应用程序中的所有表单提交事件编写一个js/jquery函数

目前,我正在3个位置上进行硬编码,就像这样,它正在工作,但我必须为每个表单分别创建一个函数:

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;
});