Jquery在$ .when()之后使用触发器点击

时间:2016-04-29 08:31:37

标签: jquery

我有一个响应编辑功能的jquery代码,然后它将触发loademployees函数并再次加载带有编辑值的表,但问题是当我在$ .when()中使用它时它会不会触发loademployees功能。为清晰起见,请查看我的代码:

$(function(){
$('#editEmployeeForm').submit(function(e){
    $.when(
        $.ajax({
            type: 'POST',
            url: "<?php echo site_url('tms/updateEmployee');?>",
            data: $(this).serialize(),
            success: function(response){
                console.log(response);
            },
        }),
        $.ajax({
            type: 'POST',
            url: "<?php echo site_url('tms/updatePersonalData');?>",
            data: $(this).serialize(),
            success: function(response){
                console.log(response);
            },
        }),
        $.ajax({
            type: 'POST',
            url: "<?php echo site_url('tms/updateGovernment');?>",
            data: $(this).serialize(),
            success: function(response){
                console.log(response);
            },
        }),
        $.ajax({
            type: 'POST',
            url: "<?php echo site_url('tms/updateRequirements');?>",
            data: $(this).serialize(),
            success: function(response){
                console.log(response);
            },
        }),
        $.ajax({
            type: 'POST',
            url: "<?php echo site_url('tms/updateContacts');?>",
            data: $(this).serialize(),
            success: function(response){
                console.log(response);
            },
        }));
        $('#modalEditEmployees').modal('hide');
        $('#btnloadEmployees').trigger(e.type);
    });
});

这是要触发的功能:

$('#btnloadEmployees').click(function(e){
var dept = $('#cmbdept').val();
if(dept == "SEWING")
{
    var section = $('#cmbsection').val();
    var line = $('#cmbline').val();
    var formData = {dSection:section,dLine:line,dept:dept};
}
else
{
    var formData = {dept:dept};
}
$.ajax({
    type: 'POST',
    url: "<?php echo site_url('tms/loadEmployees');?>",
    data: formData,
    success: function(response){
        console.log(response);
        $('#tblEmployees tbody').empty();
        $('#tblEmployees tbody').append(response);
    },
});
});

1 个答案:

答案 0 :(得分:1)

参考https://api.jquery.com/jquery.when/$.when的语法应该像

$.when( $.ajax(".."), $.ajax(".."), $.ajax("..")).done(function(){
    $('#modalEditEmployees').modal('hide');
    $('#btnloadEmployees').trigger(e.type);
});

并且您应该在阻止表单默认提交行为之前放置e.preventDefault();