简体   繁体   English

基于单选按钮选择禁用复选框上的更改值属性的问题

[英]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. 当我选择input#package-1,然后输入#highlighted时,该函数起作用。 However, it fails when I then select input#package 1 again. 但是,当我再次选择input#package 1时,它将失败。 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. checked是一个属性,而不是一个属性。 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. 另外,请尝试在禁用之前/之后重新设置值,而不是在禁用此框时进行设置。

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

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