获取所有选中的复选框 javascript 或 jquery

Fan*_*e27 0 javascript django jquery dynamic twitter-bootstrap

我想在每次用户使用 Jquery 或 javascript 单击其中一个复选框时动态获取所有选中的复选框。\nPS:我正在使用 Django + Python 生成选项并使用 Bootstrap 来设置它们的样式\n这是我的代码:

\n\n
{% for option in options %}\n    <div class="input-group" style="margin-bottom: 7px;">\n        <div class="input-group-prepend">\n            <div class="input-group-text">\n                <input type="checkbox" name="checks[]" aria-label="Radio button for following text input" id="option_choice" class="ipad_pro" value="{{ option.name }}" onclick="getCheckedCheckboxes(\'option_choice\')">\n            </div>\n        </div>\n        <input style="background-color: #fff;" type="text" class="form-control" disabled=True value="{{ option.name }} {{ option.price }}\xe2\x82\xac" aria-label="Text input with radio button">\n    </div>\n{% endfor %}\n
Run Code Online (Sandbox Code Playgroud)\n\n

到目前为止,我尝试这样做:

\n\n
function getCheckedBoxes(chkboxId) {\n  var checkboxes = document.querySelectorAll(\'#\' + chkboxId);\n  var checkboxesChecked = [];\n  for (var i=0; i<checkboxes.length; i++) {\n     if (checkboxes[i].checked) {\n        checkboxesChecked.push(checkboxes[i]);\n     }\n  }\n  return checkboxesChecked.length > 0 ? checkboxesChecked : null;\n}\nvar checkedBoxes = getCheckedBoxes("option_choice");\n
Run Code Online (Sandbox Code Playgroud)\n\n

但这对我来说还没有成功。

\n\n

如果您知道我的问题的答案,请帮助我。

\n

小智 5

使用 JS 获取所有选中的复选框输入:

var checked = document.querySelectorAll("input[type=checkbox]:checked");