简体   繁体   中英

Checkbox retaining its false attr

I have a popup, on the click of Save User i am saving a value and want to reset a form in a more generic way, so i tried using that and was successful in case of resetting the textbox, dropdown but when it came to checkbox i am facing a little trouble, i am using the below code to reset all the checkboxes:

$('#' + form).find('input[type=checkbox]:checked').each(function () {
    this.checked = false;
});

I have also tried:

$('#' + form).find('input[type=checkbox]:checked').removeAttr("checked", false);

both of the above code fails in case when i try to re-add a new user, in this case, when i checks the checkbox and found that, even after i have checked the checkboxes they all are in false state and on the form submission there values are not picked up.

But when i use:

$('input[name=storeList]').removeAttr('checked');

This code runs successfully and later on re-submission values of checkboxes are picked up accordingly.

This seems weird to me and might be to you also, but this is happening and even i dnt know why. Please help me

Note Second time checkbox shows blank value when i alert the value of checkbox.

试试这个

$('#' + form).find('input[type=checkbox]:checked').prop('checked', false);

Your case is very familiar to me because i also have been stuck in these situations many times.Why don't you can just clear the form after the form has been saved. Like this, $('form_id').form('clear');

And if possible cant u send the fiddle for this.

Looks like removeAttr() can not be reset by form.reset()

you can do this in the Following Ways :

1)

$(document).ready(function(){
    $('.check:button').toggle(function(){
        $('input:checkbox').attr('checked','checked');
        $(this).val('uncheck all');
    },function(){
        $('input:checkbox').removeAttr('checked');
        $(this).val('check all');        
    })
})

2)

$('input:checkbox').removeAttr('checked');

3)

$('input[type=checkbox]').each(function() 
{ 
        this.checked = false; 
}); 

4)

HTML

<input type="checkbox" name="all" id="checkall" />
JavaScript

$('#checkall:checkbox').change(function () {
   if($(this).attr("checked")) $('input:checkbox').attr('checked','checked');
   else $('input:checkbox').removeAttr('checked');
});​

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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