简体   繁体   中英

Issue with changing value attribute on disabled checkbox based on radio button selection

I'm having an issue editing the disabled and value attributes on a checkbox, based on which radio button is selected.

The function works when I select input#package-1 and then input#highlighted. However, it fails when I then select input#package 1 again. When I do this the value on the checkbox doesn't change (which is incorrect) but the disabled attribute is taken off (which is correct).

Any help would be much appreciated.

$(document).ready(function(){
    $('.package-select').change(function(){
       if ($('#package-2').is(':checked') || $('#package-1').is(':checked') ){
             $('input#extrasRouter').removeAttr('disabled');
             $('input#extrasRouter').val("36-Wireless router - £44.99 - TEST"); // Changing value to select the PAID FOR router 

         } else {
             $('input#extrasRouter').val("21-Wireless router - FREE");
             $('input#extrasRouter').attr('disabled', true);     
         }   
    });
});


<!-- Radio button that are being selected which should affect the below checkbox -->
        <label for="package-3" class="package-select" id="tb-package-3">
            <span class="name">Home Worker 20</span>
            <input type="radio" value="16-Home Worker 20 - &pound;35 per month" id="package-3" class="validate required" name="package">
        </label>

        <label for="highlighted" class="package-select" id="tb-highlighted">
            <span class="name">Home 20</span>
            <input type="radio" value="10-Home 20 - &pound;22 per month" id="highlighted" class="validate required" name="package">
        </label>

        <label for="package-2" class="package-select" id="tb-package-2">
            <span class="name">Home 10</span>
            <input type="radio" value="12-Home 10 - &pound;17 per month" id="package-2" class="validate required" name="package">
        </label>

        <label for="package-1" class="package-select" id="tb-package-1">
            <span class="name">Basic</span>
            <input type="radio" value="13-Basic - &pound;10 per month" id="package-1" class="validate required" name="package" >
        </label>

<!-- Checkbox that is being edited -->
<input type="checkbox" checked="" name="options[]" id="extrasRouter" value="21-Wireless router - &pound;44.99"   />

checked is a property, not an attribute. change these:

$('input#extrasRouter').removeAttr('disabled');
...
$('input#extrasRouter').attr('disabled', true);

to these:

$('input#extrasRouter').prop('disabled', false);
...
$('input#extrasRouter').prop('disabled', true);

Also try setting the values before disabling/after re-enabling instead of while the box is disabled.

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