简体   繁体   English

复选框已选中属性,但不显示为已选中

[英]checkbox has checked attribute but doesn't show checked

Something strange is going on with my code. 我的代码发生了一些奇怪的事情。 I have two buttons one with a class .add and .remove there is a checkbox that toggles on and off due to which button is pressed, so if you remove with the remove button the checked gets checked otherwise the checkbox get's unchecked, the problem I'm having is when you reuse the .remove button it adds checked attribute but the checkbox doens't show it's checked but in the html it's attribute is there. 我有两个带有类.add和.remove的按钮,其中有一个复选框会由于按下哪个按钮而打开和关闭,因此如果使用remove按钮将其选中,则选中该复选框会被选中,否则该复选框会被取消选中,这是我遇到的问题当您重用.remove按钮时,它具有已选中的属性,但复选框未显示已选中,但是在html中它的属性在那里。 does anyone know how to fix this 有谁知道如何解决这一问题

$('.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 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" />

您应该使用prop更改选中的属性。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM