如何用条件执行ajax成功函数

时间:2018-03-03 11:59:04

标签: javascript jquery

我正试图在特定条件下进入ajax成功回调。 我正在向ajax函数传递一个参数,并且基于参数匹配我试图执行函数..

我有一个if条件,如果参数是abc,那么url将是某些东西,如果参数是xyz,那么我试图跳转到成功函数。但是我无法做到。我怎么能这样做,这就是我试图做的事情。

function Call(type){
    $.ajax({
            if(type != 'xyz'){
                var url = "/param1/api/me?id="+kId+"&clickType="+type;
            }else{
                //jump to success callback
            }           
            success = function(data){
                if(type == "abc"){
                    console.log('abc');
                } else if(type == "asd"){
                    console.log('asd');
                } else if(type == "gst"){
                    console.log('gst');
                } else if(type == "xyz"){
                    console.log('xyz');
                };
            });
    });
}

2 个答案:

答案 0 :(得分:0)

喔。你的意思是这样吗?

function Call(type){
    if(type != 'xyz'){
        $.ajax({
            url: "/param1/api/me?id="+kId+"&clickType="+type,
            success: function (res) {
                console.log(res);
            }
        });
    }elseif(type == "abc"){
        console.log('abc');
    } else if(type == "asd"){
        console.log('asd');
    } else if(type == "gst"){
        console.log('gst');
    } else if(type == "xyz"){
        console.log('xyz');
    }
}

答案 1 :(得分:0)

尝试此代码。

Option Explicit

Public Sub SelectSlideArray()

    Dim ppt As Presentation
    Dim sld As Slide
    Dim textBox1 As Shape
    Dim textBox2 As Shape

    Set ppt = ActivePresentation
    Set sld = ppt.Slides(1) ' slide with text boxes in 
    Set textBox1 = sld.Shapes("TextBox 3") 'change as required
    Set textBox2 = sld.Shapes("TextBox 4") 'change as required

    textBox1.TextFrame.TextRange = 5 ' you can say TextRange.Text but .Text is defaut
    textBox2.TextFrame.TextRange = 10

    Dim startSlideNumber As Integer
    Dim endSlideNumber As Integer

    startSlideNumber = Int(textBox1.TextFrame.TextRange)
    endSlideNumber = Int(textBox2.TextFrame.TextRange)

    SelectSlides ppt, startSlideNumber, endSlideNumber

    'PrintShapeNames sld

End Sub

Public Sub SelectSlides(ByVal ppt As Presentation, ByVal startSlideNumber As Long, ByVal endSlideNumber As Long)

    Dim outputSlideNumber As Long

    outputSlideNumber = startSlideNumber

    If ppt.Slides.Count < endSlideNumber Then
        MsgBox "You don't have enough slides in the presentation!"
        End
    ElseIf endSlideNumber < startSlideNumber Then
        MsgBox "End slide is before start slide!"
        End
    Else
       Dim slidesArray()
       ReDim slidesArray(0 To endSlideNumber - startSlideNumber)
       Dim currentSlide As Long

       For currentSlide = LBound(slidesArray) To UBound(slidesArray)

           slidesArray(currentSlide) = outputSlideNumber
           outputSlideNumber = outputSlideNumber + 1

       Next currentSlide

    End If

    ppt.Slides.Range(slidesArray).Select

End Sub

Private Sub PrintShapeNames(ByVal sld As Slide)

    Dim shp As Shape

    For Each shp In sld.Shapes
        Debug.Print shp.Name  
    Next shp

End Sub