2014-10-05 66 views
0

我的代碼出現了一些奇怪的現象。我有兩個按鈕,一個是類.add和.remove,其中有一個複選框,由於按下了哪個按鈕而打開和關閉,所以如果使用刪除按鈕刪除選中的複選框,否則複選框會取消選中,問題I '當時你重複使用.remove按鈕時,它添加了checked屬性,但複選框不會顯示它已被選中,但在html中它的屬性就在那裏。沒有人知道如何解決這個問題複選框已檢查屬性,但不顯示已選中

$('.variations_grid').on('click','.add', function(e) { 
    var elements = $(this).closest('tr').find('td'); 
    var isDisabled = $(this).closest('tr').find('button.remove').is(':disabled') 

    if(isDisabled) { 
     $(this).closest('tr').removeClass('remove') 
     $(this).closest('tr').find('.add').attr('disabled', 'disabled'); 
     $(this).closest('tr').find('button.remove').removeAttr('disabled'); 
     $(this).closest('tr').find('.delete:checkbox').attr('checked', false); 
    } else { 
     $(this).closest('tr').before(template);  
    } 
    $.each(elements, function(index, element) { 
     if(index > 1) { 
      //$(this).find('select').removeAttr('disabled'); 
      //$(this).find('input').removeAttr('disabled') 
     } 
    }); 
}); 

$('.variations_grid').on('click','button.remove', function(e) { 
    var elements = $(this).closest('tr').find('td'); 

    $(this).closest('tr').addClass('remove') 
     $(this).closest('tr').find('.add').removeAttr('disabled'); 
     $(this).closest('tr').find('.remove').attr('disabled', 'disabled'); 
     $(this).closest('tr').find('.delete:checkbox').attr('checked', 'checked'); 

    $.each(elements, function(index, element) { 
     if(index > 1) { 
      //$(this).find('select').attr('disabled', 'disabled'); 
      //$(this).find('input').attr('disabled', 'disabled') 
     } 
    }); 
}); 

HTML

<button class="btn btn-default btn-block add" type="button" disabled="disabled">&nbsp;<span class="glyphicon glyphicon-plus-sign"></span>&nbsp;</button> 
<button class="btn btn-default btn-block remove" type="button">&nbsp;<span class="glyphicon glyphicon-minus-sign"></span>&nbsp;</button> 
<input name="delete" type="checkbox" class="delete" /> 

回答

2

您應該使用prop改變checked屬性。

.prop('checked' , true); // or false 
+0

啊謝謝你另一個要記住謝謝你的回答它的竅門 – ONYX 2014-10-05 06:00:40

+0

@KDM很好,很高興幫助你... – 2014-10-05 06:01:07

相關問題