结合多个表单的ajax代码

时间:2016-01-14 11:23:18

标签: javascript php jquery ajax

我有下面的两个脚本几乎做同样的事情然而是2个不同的表格集,但通过ajax发布到同一页面我的问题是有一种方法来最小化完成工作所需的代码量

例如$(document).on('submit', 'form.frm_details','form.wa_cu_del', function(event)

<script>
$(function() {
    $(document).on('submit', 'form.frm_details', function(event) {
        event.preventDefault();
        $.ajax({
            url: '/limitless/functions2.php',
            type: 'post',
            dataType: 'json',                       
            data: $(this).serialize(),
            success: function(data) {
                if(data.status == '1') {
                    $('#info').html('<div class=\"alert alert-success alert-styled-left alert-arrow-left alert-bordered\"> ' + data.message + '<button type=\"button\" class=\"close\" data-dismiss=\"alert\" >×</button></div>');
                    $('.my-modals').modal('hide');
                    webapp_get_customers(); 
                    document.getElementById('frm_details0').reset();
                } 
                if(data.status == '2') {
                    $('#info').html('<div class=\"alert alert-danger alert-styled-left alert-arrow-left alert-bordered\">' + data.message + '<button type=\"button\" class=\"close\" data-dismiss=\"alert\" >×</button></div>');
                    $('.my-modals').modal('hide');
                }                               
            }  
        });
    });
});
</script>

<script>
$(function() {
    $(document).on('submit', 'form.wa_cu_del', function(event) {
        event.preventDefault();
        $.ajax({
            url: '/limitless/functions2.php',
            type: 'post',
            dataType: 'json',                       
            data: $(this).serialize(),
            success: function(data) {
                if(data.status == '2') {
                    $('.my-modals').modal('hide');
                    $('#info').html('<div class=\"alert alert-danger alert-styled-left alert-arrow-left alert-bordered\"> ' + data.message + '<button type=\"button\" class=\"close\" data-dismiss=\"alert\" >×</button></div>');
                    webapp_get_customers(); 
                }                               
            }  
        });
    });
});
</script>

1 个答案:

答案 0 :(得分:2)

您的代码发生了变化,而不是

$(document).on('submit', 'form.frm_details','form.wa_cu_del', function(event)

你必须写

$(document).on('submit', 'form.frm_details, form.wa_cu_del', function(event)