jQuery删除ajax中的属性

Kei*_*wer 6 javascript jquery

我有一个 ajax 调用,我禁用了一个复选框,然后我想在 ajax 完成后再次启用。但是我无法删除禁用的属性。

$(function(){ // added
    $("input:checkbox").live("click", function(){

        var a_href = $(this).attr('id');
        var checked = $(this).attr('checked');
        if (checked == 'checked') {
            checked = 1;
        }else {
            checked = 0
        };

        $(this).parent().parent().after('<img class="loader" style="padding-botton:0 !important;" alt="" src="images/loaders/loader.gif">');
        $(this).attr('disabled','disabled');

        $.ajax( {
            type: "POST",
            url: "includes/featured.php",
            data: { id: a_href, value: checked, field: "main_feature" },
          success: function(data) {
            $('img.loader').fadeOut('slow',function(){$(this).remove()});
            $(this).removeAttr('disabled'); 
          }             
        }).done(function (data){
            }
        );

    return false; 


    });
}); // added
Run Code Online (Sandbox Code Playgroud)

我也试过:

.attr('disabled', false);
Run Code Online (Sandbox Code Playgroud)

gdo*_*ica 6

您需要保存对的引用,this因为thisajax 回调内部是 ajax 请求。

$("input:checkbox").live("click", function(){
    var $this = $(this);
    ...
    ...
    success: function(data) {
        $('img.loader').fadeOut('slow',function(){$(this).remove()});
        $this.removeAttr('disabled');
Run Code Online (Sandbox Code Playgroud)

或设置contextthis

    $.ajax( {
        type: "POST",
        context: this, // <===============
        url: "includes/featured.php",
        data: { id: a_href, value: checked, field: "main_feature" },
      success: function(data) {
          // Now this is the checkbox!
        $('img.loader').fadeOut('slow',function(){$(this).remove()});
        $(this).removeAttr('disabled'); 
      }  
Run Code Online (Sandbox Code Playgroud)