使用Ajax选项调用两个函数

时间:2017-12-12 17:17:58

标签: javascript jquery ajax asp.net-mvc

我试图在表单提交时使用AjaxOptions运行两个函数。但是,只调用了一个函数。我希望在提交表单时运行它们。

这是我的ajax选项:

 @using (Ajax.BeginForm("AddDiagnosis", "Student",

            new AjaxOptions(){
                OnSuccess = "HandleAddDiagnosisCode",
                OnBegin = "return ValidateAddStudentDiagnosisCode(" + @studentId + ")"

            }, new
            {
                id = "AddStudentDiagnosis-" + @studentId,
                name = "AddStudentDiagnosis-" + @studentId
            }
            ))
        {

这些是功能:

function ValidateAddStudentDiagnosisCode(id) {   
    var valid = true;    
    var msg = "";
    var errorPanel = $('#modal-AddStudentDiagnosis-ErrorPanel');
    PopulateErrorMessage(errorPanel, msg);
    errorPanel.hide();
    var diagCode = $('#modal-AddStudentDiagnosis_DiagCode-' + id).val();

    if (diagCode === "") {
        msg = "You must select a diagnosis code";
        valid = false;
    }
    var sequence = $('#modal-AddStudentDiagnosis_Sequence-' + id).val();
    if (sequence === "") {
        msg = "You must select a sequence";
        valid = false;        
    }
    if (valid == false) {
        PopulateErrorMessage(errorPanel, msg);      
    }    
    return valid;
}

    function HandleAddDiagnosisCode(e) {   
        alert('test');
        var errorPanel = $('#modal-AddStudentDiagnosis-ErrorPanel');
        PopulateErrorMessage(errorPanel, '');
        var errorMessage = e.ErrorMessage;
        if (errorMessage.length > 0) {
            PopulateErrorPanel(errorPanel, errorMessage);
        }
        else {
            ClearModal();        
            // Reload the document view
            $('#detailPanel').fadeOut(500, function () {
                $(this).empty();
            }).fadeIn(250, function () {
                $(this).html(e.Html);
                BindDocumentEdit();
                BindDataTable();
                BindDatePickers();            
            });
        }

提交表单时不会调用我的测试提醒。

1 个答案:

答案 0 :(得分:0)

您可以使用以下匿名函数包装函数调用:

@using (@Ajax.BeginForm("AddDiagnosis", "Student", new AjaxOptions
    {
        OnSuccess = "new function(){func1(xhr); func2();}"
    })