英文:
Get checked checkbox value on array
问题
I have data value in form input. How to retrieve checked checkbox state above and hold it in an array.
function test() {
var main = [];
var cboxes = document.getElementsByName('mailId[]');
var len = cboxes.length;
for (var i = 0; i < len; i++) {
var cok = (i + (cboxes[i].checked ? ' checked ' : ' unchecked ') + cboxes[i].value);
console.log(cok);
// if checked main.push(value)
// else delete value in array main
}
}
I made a log, but the log doesn't take the value from the checkbox above.
If the checkbox is checked, then the value will be entered into the array. If it is unchecked, the value will be deleted from the array storage.
英文:
I have data value in form input. How to retrieve checked checkbox state above and hold it in an array.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function test() {
var main = [];
var cboxes = document.getElementsByName('mailId[]');
var len = cboxes.length;
for (var i = 0; i < len; i++) {
var cok = (i + (cboxes[i].checked ? ' checked ' : ' unchecked ') + cboxes[i].value);
console.log(cok);
// if checked main.push(value)
// else delete value in array main
}
}
<!-- language: lang-html -->
<th><input type="checkbox" class="messageCheckbox" id="checkbox1" value="13" name="mailId[]" onClick="test()"></th>
<th><input type="checkbox" class="messageCheckbox" id="checkbox1" value="14" name="mailId[]" onClick="test()"></th>
<!-- end snippet -->
I made a log, but the log doesn't take the value from the checkbox above.
If the check box is checked then the value will be entered into the array, if it is unchecked the value will be deleted from the array storage
答案1
得分: 1
从复选框创建数组。单击时,检查复选框状态,根据状态将值推送到数组或从数组中删除。
示例:
let main = {};
main['value'] = [];
function test() {
var checkbox = Array.from(document.getElementsByName("mailId[]"));
for (var i = 0; i < checkbox.length; i++) {
var sbVal = checkbox[i].value;
if (checkbox[i].checked && main.value.indexOf(sbVal) < 0) {
main.value.push(checkbox[i].value);
continue;
}
if (checkbox[i].checked === false && main.value.indexOf(sbVal) >= 0) {
var j = main.value.indexOf(sbVal);
main.value.splice(j, 1);
}
}
console.log(main['value']);
}
<input type="checkbox" class="messageCheckbox" id="checkbox1" value="13" name="mailId[]" onClick="test()">
<input type="checkbox" class="messageCheckbox" id="checkbox1" value="14" name="mailId[]" onClick="test()">
注意:id必须是唯一的,否则违反规则。
英文:
Creates array from checkboxes. On click, check for checkbox status, based upon that push or splice that value from the array.
Example:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let main = {};
main['value'] = [];
function test() {
var checkbox = Array.from(document.getElementsByName("mailId[]"));
for (var i = 0; i < checkbox.length; i++) {
var sbVal = checkbox[i].value;
if (checkbox[i].checked && main.value.indexOf(sbVal) < 0) {
main.value.push(checkbox[i].value);
continue;
}
if (checkbox[i].checked === false && main.value.indexOf(sbVal) >= 0) {
var j = main.value.indexOf(sbVal);
main.value.splice(j, 1);
}
}
console.log(main['value']);
}
<!-- language: lang-html -->
<input type="checkbox" class="messageCheckbox" id="checkbox1" value="13" name="mailId[]" onClick="test()">
<input type="checkbox" class="messageCheckbox" id="checkbox1" value="14" name="mailId[]" onClick="test()">
<!-- end snippet -->
Note: id must be unique otherwise it's rule violation.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论