6

チェックボックスを無効にする 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

私も試しました:

.attr('disabled', false);
4

4 に答える 4

6

ajax コールバック内に ajax リクエストがある thisため、参照を保存する必要があります。this

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

または を に設定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'); 
      }  
于 2012-05-01T19:35:39.723 に答える
3

thisAJAX 成功ハンドラーでは、チェックボックスにはなりません。最初にその変数をキャッシュする必要があります。その後、成功ハンドラーで使用できます。また、属性を削除する場合はproporを使用することをお勧めします。removePropこれを試して:

$(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;
    });
});
于 2012-05-01T19:34:10.770 に答える
2

成功関数内の「これ」は、あなたが考えているものではないと思います。チェックボックスの ajax スコープ外の変数を設定してから、success 関数内でそれを参照してみてください。

于 2012-05-01T19:34:28.587 に答える
2

$(this)チェックボックスを再度有効にしようとすると、範囲外になります。

var newthis = $(this)後に追加

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

呼び出しを変更して、「無効な」属性を削除します

newthis.removeAttr('disabled');

于 2012-05-01T19:35:36.197 に答える