Jquery 如何在ajax返回错误时更新materialize开关元素?

Jquery 如何在ajax返回错误时更新materialize开关元素?,jquery,materialize,Jquery,Materialize,如何更新交换机状态?此ajax错误不会更新代码中的materialize开关 $(document).on('change','.setStatus',function(){ if($(this).prop('checked')==true){ var activeStatus=1; }else{ var activeStatus=0; } $.ajax({ type:'POST', u

如何更新交换机状态?此ajax错误不会更新代码中的materialize开关

$(document).on('change','.setStatus',function(){    
    if($(this).prop('checked')==true){
        var activeStatus=1;     
    }else{
        var activeStatus=0;     
    }

    $.ajax({
    type:'POST',
    url:'service.php',
    data:{'status':activeStatus,'userId':userId},
    success:function(data){
    },          
    error:function(){
        if(activeStatus==1){
            $(this).prop('checked',false);      // but this not update.                         
        }else{
            $(this).prop('checked',true);   // but this not update.                     
        }
    }               
   });
});
错误回调中的$(this)似乎不再引用switch元素。可以将开关元素存储在变量中,然后使用该变量更改道具。也许是这样的:

$(document).on('change', '.setStatus', function () {
    var activeStatus = 0;
    var $switch = $(this);

    if ($(this).prop('checked') == true) {
        activeStatus = 1;
    }

    $.ajax({
        type: 'POST',
        url: 'service.php',
        data: {'status': activeStatus, 'userId': userId},
        success: function (data) {
        },
        error: function () {
            if (activeStatus == 1) {
                $switch.prop('checked', false);      // but this not update.                         
            } else {
                $switch.prop('checked', true);   // but this not update.                     
            }
        }
    });
});
$(这个)在回调中不起作用。所以将$(this)存储在变量中以进行更新。像

$(document).on('change','.setStatus',function(){    
    var this1 = $(this);
    if(this1.prop('checked')==true){
        var activeStatus=1;     
    }else{
        var activeStatus=0;     
    }

    $.ajax({
    type:'POST',
    url:'service.php',
    data:{'status':activeStatus,'userId':userId},
    success:function(data){
    },          
    error:function(){
        if(activeStatus==1){
            this1.prop('checked',false);      // this will update.                         
        }else{
            this1.prop('checked',true);   // this will update.                     
        }
    }               
   });
});
也可以使用上下文变量。。默认情况下,上下文是表示调用中使用的ajax设置的对象。所以在回调中,它将引用当前上下文。你可以像我一样

$(document).on('change','.setStatus',function(){    
    if($(this).prop('checked')==true){
        var activeStatus=1;     
    }else{
        var activeStatus=0;     
    }

    $.ajax({
    type:'POST',
    url:'service.php',
    context: $(this),
    data:{'status':activeStatus,'userId':userId},
    success:function(data){
    },          
    error:function(){
        if(activeStatus==1){
            $(this).prop('checked',false);      //  this will update.                         
        }else{
            $(this).prop('checked',true);   //  this will update.                     
        }
    }               
   });
});

你可以阅读更多关于上下文的内容

这对你来说很好???