Javascript 使用单选按钮填充复选框列表

Javascript 使用单选按钮填充复选框列表,javascript,python,jquery,Javascript,Python,Jquery,我试图通过单击单选按钮,在以下for循环创建的复选框中填充一组报告id <div class="col"><h4 style="margin-top: 0"><strong>Application Suite</strong></h4><input type="radio" id="radio1" name="suite" value="1" onclick="CheckBoxes">>Executive CFO Su

我试图通过单击单选按钮,在以下for循环创建的复选框中填充一组报告id

<div class="col"><h4 style="margin-top: 0"><strong>Application Suite</strong></h4><input type="radio" id="radio1" name="suite" value="1" onclick="CheckBoxes">>Executive CFO Suite</div>
<div class="container">

        <div class="col-md-4">
          {% if fingrouplist is not None %}
                      <h4><strong>Financial</strong/></br></br><input type="checkbox" onClick="togglerep(this)" /> Select All</h4>

                              <ul>
                              {% for app in fingrouplist %}
                              <li><input type="checkbox" name="report_id" value ="{{app.report_id}}" >  {{ app.report_name_sc }}</li>
                              {% endfor %}
                              </ul>
          {% endif %}
        </div>
有时,report\u id列表并不总是包含所有选项,但我只想在从for循环派生的report\u id中包含用户设置中存在的选项

我还创建了一个python对象来定义报告列表,因此如果我可以从该选项中获得它,而不是键入数字列表,那么这可能是一个更好的方法

rolebased = QvReportList.objects.filter(role_based_id__exact = '1').values('report_id', 'report_name_sc').distinct()

非常感谢您为我提供的任何帮助。

您的代码中有很多错误。首先,您应该使用
()
来调用函数,而不仅仅是它的名称,因此您需要像这样更改单选按钮:

<input type="radio" id="radio1" name="suite" value="1" onclick="CheckBoxes(this)">
作为旁注,如果要提交一个包含多个同名复选框的表单,则应使用数组名称定义
[]
。在您的情况下,它将是(注意名称定义中方括号的使用):


  • 值1
  • 价值2
  • 价值3
  • 价值4
  • 价值5

您想要实现什么目标?获取所有选中的复选框值?单击单选按钮时,我正在尝试检查表单中的那些报告id。我不确定要搜索什么,因为我找不到一个简单的例子。让我看看我是否得到了它,当你单击单选按钮时,你需要选中一些复选框,对吗?你是否尝试添加所有复选框的id<代码>$(“#checkbox_id_1,#checkbox_id_2,#checkbox_id_3,…,#checkbox_id_60”)。道具('checked',true)感谢您的指导。
<input type="radio" id="radio1" name="suite" value="1" onclick="CheckBoxes(this)">
function CheckBoxes(clicked) {

    // array of checkboxes to check
    var checked = [15, 16, 17, 18, 19, 22, 23, 26, 28, 29, 30, 31, 33, 34, 35, 36, 39, 40, 47, 48, 50, 52, 59, 60];

    // if the radio button is checked
    if($(clicked).is(':checked')) {

        // for each checked checkbox
        $('input[name*="report_id"]').each(function() {

            // set as checked if the value is defined in the array
            // Convert $(this).val() to Numnber() to compare against a number
            $(this).prop('checked', checked.indexOf(Number($(this).val())) >= 0);

        });

    }

}
<input type="checkbox" name="report_id[]" value ="{{app.report_id}}" >