简体   繁体   English

使用.removeAttr('checked')不起作用

[英]Using .removeAttr('checked') does not work

I use of codeigniter.see you: example of my code please fill textbox and checked checkbox next clicked on link add, see yourself that not work .removeAttr('checked') 我使用codeigniter.see: 代码示例,请填写文本框并选中复选框,然后单击添加的链接,看看自己不起作用.removeAttr('checked')

newDiv.find('input').each(function () { $(this).removeAttr('checked').val(''); });


full code: 完整代码:

$(document).ready(function () {
    $(function () {
        $('a.add').live('click', function (event) {
            event.preventDefault();
            var $class = '.' + $(this).closest('div.find_input').find('div').attr('class');
            var newDiv = $(this).closest($class).clone();
            $(this).closest($class).find('.adda').remove()
            //$(this).find('.adda').remove()
            //newDiv.append('<a href="" class="remove_input"></a>')
            newDiv.find('input').each(function () {
                $(this).removeAttr('checked').val('');
            });
            $($class + ':last').after(newDiv);
            //newDiv.remove('.adda')
            //alert(newDiv)
        });

        $('a.remove_input').live('click', function (event) {
            event.preventDefault();
            $(this).closest('div').remove();
        });
    });
});

With respect 尊重地

您应该通过以下方式使用prop函数:

$(this).prop('checked', false);

尝试这个:

newDiv.find('input').each(function () { $(this).prop('checked', false).val(''); });

I think it's a mistake to set the value after removing the attr. 我认为删除attr后设置值是错误的。

I've had better luck doing this: 我最好这样做:

$(this).attr('checked', false);

A checkbox's checked state is inseparable from the element; 复选框的checked状态与元素是分不开的。 you can no more remove its is-checked/is-not-checked state than you can remove it's HTML-element-ness. 您再也无法删除其HTML-element-ness,因此无法删除其is-checked / is-not-checked状态。 Instead, indicate that the checkbox should be unchecked. 相反,请指示应取消选中该复选框。

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

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