每次用户使用 Jquery 或 javascript 单击其中一个复选框时,我想动态地将所有选中的复选框放入一个数组中。PS:我使用 Django + Python 来生成选项和 Bootstrap 来设置它们的样式这是我的代码:
{% for option in options %}
<div class="input-group" style="margin-bottom: 7px;">
<div class="input-group-prepend">
<div class="input-group-text">
<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')">
</div>
</div>
<input style="background-color: #fff;" type="text" class="form-control" disabled=True value="{{ option.name }} {{ option.price }}€" aria-label="Text input with radio button">
</div>
{% endfor %}
到目前为止,我已经尝试这样做:
function getCheckedBoxes(chkboxId) {
var checkboxes = document.querySelectorAll('#' + chkboxId);
var checkboxesChecked = [];
for (var i=0; i<checkboxes.length; i++) {
if (checkboxes[i].checked) {
checkboxesChecked.push(checkboxes[i]);
}
}
return checkboxesChecked.length > 0 ? checkboxesChecked : null;
}
var checkedBoxes = getCheckedBoxes("option_choice");
但这对我没有用。
如果您知道我的问题的答案,请帮助我。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句