最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery Remove attribute in ajax - Stack Overflow

programmeradmin2浏览0评论

I have an ajax call that I disable a checkbox and I then want to enable again once the ajax has finished. However I cannt remove the attribute disabled aterwards.

$(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

I have also tried:

.attr('disabled', false);

I have an ajax call that I disable a checkbox and I then want to enable again once the ajax has finished. However I cannt remove the attribute disabled aterwards.

$(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

I have also tried:

.attr('disabled', false);
Share Improve this question asked May 1, 2012 at 19:32 Keith PowerKeith Power 14.2k23 gold badges70 silver badges140 bronze badges 0
Add a ment  | 

4 Answers 4

Reset to default 6

You need to save a reference to this because this inside ajax callback is the ajax request.

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

or set the context to this:

    $.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'); 
      }  

this in the AJAX success handler will not be the checkbox. You'll need to cache that variable first, then you can use it in the success handler. It's also worth noting that it's better to use prop or removeProp when removing attributes. Try this:

$(function(){ // added
    $("input:checkbox").live("click", function(){
        var $checkbox = $(this);

        var a_href = $checkbox.attr('id');
        var checked = $checkbox.attr('checked');
        checked = (checked == "checked") ? 1 : 0;

        $checkbox.parent().parent().after('<img class="loader" style="padding-botton:0 !important;" alt="" src="images/loaders/loader.gif">');
        $checkbox.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()});
                $checkbox.removeProp('disabled'); 
            }             
        }).done(function (data) {});

        return false;
    });
});

I don't think 'this' inside the success function is what you think it is. Try setting a variable outside the ajax scope for your checkbox, and then referencing that inside the success function.

Your $(this) is out of scope when you try to re-enable your checkbox.

Add var newthis = $(this) after

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

and change your call to remove the 'disabled' attr to

newthis.removeAttr('disabled');

发布评论

评论列表(0)

  1. 暂无评论