[英]checkbox has checked attribute but doesn't show checked
我的代碼發生了一些奇怪的事情。 我有兩個帶有類.add和.remove的按鈕,其中有一個復選框會由於按下哪個按鈕而打開和關閉,因此如果使用remove按鈕將其選中,則選中該復選框會被選中,否則該復選框會被取消選中,這是我遇到的問題當您重用.remove按鈕時,它具有已選中的屬性,但復選框未顯示已選中,但是在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"> <span class="glyphicon glyphicon-plus-sign"></span> </button>
<button class="btn btn-default btn-block remove" type="button"> <span class="glyphicon glyphicon-minus-sign"></span> </button>
<input name="delete" type="checkbox" class="delete" />
您應該使用prop
更改選中的屬性。
.prop('checked' , true); // or false
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.