简体   繁体   中英

Toggling of radio button only works once (refresh)?

I am trying to toggle a radio box selection included in cells table.

The toggling works only once per radio button.

After, all radio buttons are disabled but my HTML code seems to be correct; the attribute checked="checked" is present.

Why is this not working?

Here is the jQuery I am using:

$('td').click(function(){
    var $elem = $(this).children('input');               
    var name = $elem.attr('name');
    $('input[name='+name+']').each(function(){
       $(this).removeAttr('checked');                  
    }).promise().done(function(){                 
       $elem.attr('checked','checked');               
    });
}); 

And the image:

在此处输入图片说明

You need to be using prop() as currently you are actually removing the attribute using removeAttr() instead of just setting it to disabled.

The following will work:

$('input[name='+name+']').each(function(){
   $(this).prop('checked', false);                  
}).promise().done(function(){                 
   $elem.prop('checked', true);               
});

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