如何在Ajax之外访问textStatus

时间:2020-01-02 14:51:36

标签: javascript ajax

我想在ajax之后访问textStatus。现在,我想在条件是否有人帮助我的情况下检查此变量 $ this-> registerJs(“ colorArray = ['#ff4c4c','#32CD32'];

    $('.grooveTable').on('click','td', function(){
    color = $(this).data('color') == undefined ? 0 : $(this).data('color')*2;

        // Get Url Parameter

        var result = [];
        window.location.search.replace(/[?&]+([^=&]+)=([^&]*)/gi, 
        function(str, key, value) {
            result[key] = value;
        });

    $.ajax({
    url: '" . yii\helpers\Url::toRoute("area-chart/change-area-status") . "',
    dataType: 'json',
    method: 'GET',
    data: {areaCodeId : $(this).attr('id'),colourCode:color, location:result['tion%5D'],
    company_code:result['ny_code%5D'],division_code:result['rt%5Bdivision_code%5D']},
    success: function (data, textStatus, jqXHR) {
    statusCheck = data;
    drawChart(data.idCompleted, data.idPending, data.idStarted);
    },});

        if(textStatus == 'SUCCESS' && color == undefined || color == colorArray.length ){

                $(this).css('background-color',colorArray[0]);
                $(this).data('color','0');

        }else if(textStatus == 'SUCCESS' == 'UPDATE'){

                $(this).css('background-color',colorArray[color+1]);
                $(this).data('color',color+1);

        }

});“);

2 个答案:

答案 0 :(得分:2)

ajax是异步的。 只需将参数传递给您的函数

$.ajax({
    url: '" . yii\helpers\Url::toRoute("area-chart/change-area-status") . "',
    dataType: 'json',
    method: 'GET',
    data: {
        areaCodeId: $(this).attr('id'), colourCode: color, location: result['tion%5D'],
        company_code: result['ny_code%5D'], division_code: result['rt%5Bdivision_code%5D']
    },
    success: function (data, textStatus, jqXHR) {
        drawChart(data.idCompleted, data.idPending, data.idStarted);
        doSomething(textStatus);
    },
});


function doSomething(textStatus){
    if(textStatus == 'SUCCESS' && color == undefined || color == colorArray.length ){

        $(this).css('background-color',colorArray[0]);
        $(this).data('color','0');

    }else if(textStatus == 'SUCCESS'){

        $(this).css('background-color',colorArray[color+1]);
        $(this).data('color',color+1);

    }
}

答案 1 :(得分:0)

尝试以下代码:

   $.ajax({
  url: '" . yii\helpers\Url::toRoute("area-chart/change-area-status") . "',
  dataType: 'json',
  method: 'GET',
  data: {
    areaCodeId: $(this).attr('id'),
    colourCode: color,
    location: result['tion%5D'],
    company_code: result['ny_code%5D'],
    division_code: result['rt%5Bdivision_code%5D']
  },
  success: function(data, textStatus, jqXHR) {
    drawChart(data.idCompleted, data.idPending, data.idStarted);
updatebackground($(this),textStatus, color, colorArray); // call function here
  },
});

将所有代码移至单独的功能:

funtion updatebackground(element,textStatus, color, colorArray){
if (textStatus == 'SUCCESS' && color == undefined || color == colorArray.length) {

  element.css('background-color', colorArray[0]);
  element.data('color', '0');

} else if (textStatus == 'SUCCESS') {

  element.css('background-color', colorArray[color + 1]);
  element.data('color', color + 1);

}
}

谢谢