简体   繁体   中英

How to enable all the radio button with a checkbox by using javascript?

I have 8 labels and each of them has 5 radio buttons(1-bad,2,3,4,5-good) and the user should select top 5.I have already figure out to disable rest of buttons when user already chosen 5 radio button here is the code,

                  1     2     3     4     5      
apple             o     o     o     o     o
banana            o     o
orange
onion
potato
grape
peach
cherry

<script>

    $( document ).ready(function() {

    $('input[type="radio"]').change(function(){
        var l = $('input[type="radio"]:checked').length;
        if( l >= 6 ) {
            $(this).prop('checked', false);
            for(var i=0; i < $('input[type="radio"]').length; i++) {
                if( !$('input[type="radio"]').eq(i).is(':checked') ) {
                    $('input[type="radio"]').eq(i).prop('disabled', 'disabled');
                }
            }
        }
    });

if (document.getElementById('edit-submitted-enable-1').checked){

$("input[type=radio]").attr('enabled', true);
}


});




</script>

How ever, If the user would like to edit his choice there must be "enable all radio buttons" I am trying to enable all radio button on the page with a checkbox how ever the code didnt work

if (document.getElementById('edit-submitted-enable-1').checked){

$("input[type=radio]").attr('enabled', true);
}

Radio buttons have property called checked . You change use event handler on "enable all radio buttons", something like

  $('.enable-all-radios').on("click", function() {
     $("input[type='radio']").prop("checked", false)
  });

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