获取数组中已选中复选框的值

huangapple go评论68阅读模式
英文:

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(&#39;mailId[]&#39;);
  var len = cboxes.length;
  for (var i = 0; i &lt; len; i++) {
    var cok = (i + (cboxes[i].checked ? &#39; checked &#39; : &#39; unchecked &#39;) + cboxes[i].value);
    console.log(cok);
    // if checked main.push(value)
    // else delete value in array main

  }
}

<!-- language: lang-html -->

&lt;th&gt;&lt;input type=&quot;checkbox&quot; class=&quot;messageCheckbox&quot; id=&quot;checkbox1&quot; value=&quot;13&quot; name=&quot;mailId[]&quot; onClick=&quot;test()&quot;&gt;&lt;/th&gt;

&lt;th&gt;&lt;input type=&quot;checkbox&quot; class=&quot;messageCheckbox&quot; id=&quot;checkbox1&quot; value=&quot;14&quot; name=&quot;mailId[]&quot; onClick=&quot;test()&quot;&gt;&lt;/th&gt;

<!-- 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[&#39;value&#39;] = [];

function test() {
  var checkbox = Array.from(document.getElementsByName(&quot;mailId[]&quot;));

  for (var i = 0; i &lt; checkbox.length; i++) {
    var sbVal = checkbox[i].value;

    if (checkbox[i].checked &amp;&amp; main.value.indexOf(sbVal) &lt; 0) {
      main.value.push(checkbox[i].value);
      continue;
    }

    if (checkbox[i].checked === false &amp;&amp; main.value.indexOf(sbVal) &gt;= 0) {
      var j = main.value.indexOf(sbVal);
      main.value.splice(j, 1);
    }
  }

  console.log(main[&#39;value&#39;]);
}

<!-- language: lang-html -->

&lt;input type=&quot;checkbox&quot; class=&quot;messageCheckbox&quot; id=&quot;checkbox1&quot; value=&quot;13&quot; name=&quot;mailId[]&quot; onClick=&quot;test()&quot;&gt;

&lt;input type=&quot;checkbox&quot; class=&quot;messageCheckbox&quot; id=&quot;checkbox1&quot; value=&quot;14&quot; name=&quot;mailId[]&quot; onClick=&quot;test()&quot;&gt;

<!-- end snippet -->

Note: id must be unique otherwise it's rule violation.

huangapple
  • 本文由 发表于 2023年4月20日 00:05:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76056651.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定