Webapp-适用于多种表单的动态Ajax


Webapp - Dynamic Ajax for multiple forms

我正在构建一个网络应用程序来跟踪一些客户的详细信息和其他各种信息

我追求的是一个简单的ajax函数,我可以在同一页面中多次重用它,将数据从各种表单(如新客户表单)发送到另一个页面,并说是新的潜在客户表单,还是我需要为每个创建不同的ajax函数

我的登录页面上有这个演示代码,但它适用于特定的表单,我希望它能够被赋予一个不同的表单名称,并将该表单中的所有字段提交到另一个页面,这可能是吗

<script type='text/javascript'>
    $('#form').on('submit',function(event){
        event.preventDefault();
        var wa_username = $('#wa_username').val();
        var wa_password = $('#wa_password').val();
        var datas='wa_username='+wa_username+'&wa_password='+wa_password;     
        $.ajax({
                type: 'POST',
                url: '/limitless/functions.php',
                dataType: 'json',
               data: datas,
                           success: function(data) {
                               if(data.status == '1')
                                {
                                    document.location.href = '/limitless/dashboard';
                                } 
                               if(data.status == '2')
                                {
                                    $('#info').addClass('alert alert-danger no-border').html(data.message);
                                }                               
                            }              
            })
    });
</script>

表单上的.serialize()方法以标准URL编码表示法创建文本字符串。

 $(this).serialize() //this produces wa_username=test&wa_password=123

你可以像下面这样拆分它,或者不创建数据,只需调用上方的线路

$('#form').on('submit',function(event){
        event.preventDefault();         
        postForm($(this).serialize());
});
function postForm(formData){
  $.ajax({
                type: 'POST',
                url: '/limitless/functions.php',
                dataType: 'json',
               data: formData,
                           success: function(data) {
                               if(data.status == '1')
                                {
                                    document.location.href = '/limitless/dashboard';
                                } 
                               if(data.status == '2')
                                {
                                    $('#info').addClass('alert alert-danger no-border').html(data.message);
                                }                               
                            }              
            })
}