简体   繁体   English

选择复选框并使用jQuery启用相应的单选按钮

[英]Select checkboxes and enable corresponding radio button using jQuery

I have a checkbox list which contains 6 checkboxes. 我有一个包含6个复选框的复选框列表。 A corresponding radio button is placed beside each checkbox. 相应的单选按钮位于每个复选框旁边。 When page load, the radio buttons are disabled. 页面加载时,单选按钮被禁用。 When I check the checkbox, I want to enable the radio button beside it. 选中该复选框时,我想启用其旁边的单选按钮。 Furthermore, if I uncheck the checkboxes, the radio button beside it will be deselected and disabled again. 此外,如果我取消选中复选框,则旁边的单选按钮将被取消选择并再次禁用。

The checkbox is for the user to choose items they want, and the radio button is for the user to pick his favourite item. 复选框供用户选择所需的项目,单选按钮供用户选择自己喜欢的项目。

I have tried this code below but it is not working... 我已经在下面尝试过此代码,但无法正常工作...

 $(document).ready(function () {
     $("#rbtnlLeadAgencies input[type='radio']").each(function () {
         $("#rbtnlLeadAgencies input[type='radio']").prop('disabled', true);
     });
     $("#cbxlAgencies input[type='checkbox']").each.change(function () {
         if ($("#cbxlAgencies_0 input[type='checkbox']").is(':checked')) $("#rbtnlLeadAgencies_0 input[type='radio']").prop('disabled', false);
         if ($("#cbxlAgencies_1 input[type='checkbox']").is(':checked')) $("#rbtnlLeadAgencies_1 input[type='radio']").prop('disabled', false);
         if ($("#cbxlAgencies_2 input[type='checkbox']").is(':checked')) $("#rbtnlLeadAgencies_2 input[type='radio']").prop('disabled', false);
         if ($("#cbxlAgencies_3 input[type='checkbox']").is(':checked')) $("#rbtnlLeadAgencies_3 input[type='radio']").prop('disabled', false);
         if ($("#cbxlAgencies_4 input[type='checkbox']").is(':checked')) $("#rbtnlLeadAgencies_4 input[type='radio']").prop('disabled', false);
         if ($("#cbxlAgencies_5 input[type='checkbox']").is(':checked')) $("#rbtnlLeadAgencies_5 input[type='radio']").prop('disabled', false);
     });

 });

The markups and jQuery code are on jsfiddle 标记和jQuery代码在jsfiddle上

Any ideas of achieving this behavior? 有实现这种行为的想法吗?

Thanks in advance! 提前致谢!

DEMO 演示

 $(document).ready(function () {
     $('input:radio[id^="rbtnlLeadAgencies_"]').prop('disabled', true);
     $('input:checkbox[id^="cbxlAgencies"]').change(function () {
         var id = this.id.replace('cbxlAgencies_', '');
         $("#rbtnlLeadAgencies_" + id).prop('disabled', !this.checked);
     });
 });

^ attribute-starts-with-selector ^属性从选择器开始

Try 尝试

 $(document).ready(function () {
     $("#rbtnlLeadAgencies input[type='radio']").prop('disabled', true);

     $("#cbxlAgencies input[type='checkbox']").change(function () {
         var idx = $(this).closest('tr').index();
         var radios = $("#rbtnlLeadAgencies tr").eq(idx).find('input[type="radio"]').prop('disabled', !this.checked);

         if(!this.checked){
             radios.prop('checked', false);
         }
     });

 });

Demo: Fiddle 演示: 小提琴

As I said in my comment here is the solution which is a bit expensive in markup but more generic : 正如我在评论中所说的那样,这里的解决方案在标记方面有点昂贵,但更为通用:

HTML 的HTML

<input id="cbxlAgencies_0" data-for-radio="rbtnlLeadAgencies_0" type="checkbox"/>

JS JS

$(document).ready(function () {
    $("input[type='checkbox'][data-for-radio]").change(function () {
        $('#' + $(this).data('for-radio')).prop('disabled', !this.checked);
    }).change();

});

http://jsfiddle.net/techunter/7M54h/ http://jsfiddle.net/techunter/7M54h/

Try jsfiddle code . 尝试jsfiddle代码

$(document).ready(function () {
     $("#rbtnlLeadAgencies input[type='radio']").attr('disabled', true);
     $("#cbxlAgencies input[type='checkbox']").change(function () {
          $("#rbtnlLeadAgencies_0").prop('disabled', !this.checked);
          $("#rbtnlLeadAgencies_1").prop('disabled', !this.checked);
          $("#rbtnlLeadAgencies_2").prop('disabled', !this.checked);
          $("#rbtnlLeadAgencies_3").prop('disabled', !this.checked);
          $("#rbtnlLeadAgencies_4").prop('disabled', !this.checked);
          $("#rbtnlLeadAgencies_5").prop('disabled', !this.checked);
     });

});

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

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