我有这些复选框:

<input type="checkbox" name="type" value="4" />
<input type="checkbox" name="type" value="3" />
<input type="checkbox" name="type" value="1" />
<input type="checkbox" name="type" value="5" />

等等。它们中大约有6个是手工编码的(即不是从db中获取的),所以它们可能会在一段时间内保持相同。

我的问题是我如何能得到他们都在一个数组(javascript),所以我可以使用他们,而使AJAX $。使用Jquery发布请求。

任何想法吗?

编辑:我只希望将选定的复选框添加到数组中


当前回答

var array = []
    $("input:checkbox[name=type]:checked").each(function(){
        array.push($(this).val());
    });

其他回答

 var checked= $('input[name="nameOfCheckbox"]:checked').map(function() {
   return this.value;
}).get();

纯JS

对于那些不想使用jQuery的人

var array = []
var checkboxes = document.querySelectorAll('input[type=checkbox]:checked')

for (var i = 0; i < checkboxes.length; i++) {
  array.push(checkboxes[i].value)
}

如果你想使用一个普通的JS,你可以像@zahid-ullah那样做,但要避免循环:

  var values = [].filter.call(document.getElementsByName('fruits[]'), function(c) {
    return c.checked;
  }).map(function(c) {
    return c.value;
  });

ES6中的相同代码看起来更好:

var values = [].filter.call(document.getElementsByName('fruits[]'), (c) => c.checked).map(c => c.value);

window.serialize = function serialize() { var values = [].filter.call(document.getElementsByName('fruits[]'), function(c) { return c.checked; }).map(function(c) { return c.value; }); document.getElementById('serialized').innerText = JSON.stringify(values); } label { display: block; } <label> <input type="checkbox" name="fruits[]" value="banana">Banana </label> <label> <input type="checkbox" name="fruits[]" value="apple">Apple </label> <label> <input type="checkbox" name="fruits[]" value="peach">Peach </label> <label> <input type="checkbox" name="fruits[]" value="orange">Orange </label> <label> <input type="checkbox" name="fruits[]" value="strawberry">Strawberry </label> <button onclick="serialize()">Serialize </button> <div id="serialized"> </div>

你可以尝试这样做:

$('input[type="checkbox"]').change(function(){
       var checkedValue = $('input:checkbox:checked').map(function(){
                return this.value;
            }).get();         
            alert(checkedValue);   //display selected checkbox value     
 })

Here

$('input[type="checkbox"]').change(function() call when any checkbox checked or unchecked, after this
$('input:checkbox:checked').map(function()  looping on all checkbox,

$(document).ready(function() { $('input[type="checkbox"]').click(function() { var arr =[]; $('input[type="checkbox"]:checked').each(function() { //arr.push($(this).parent('p').text()+'\n'); arr.push($(this).val()+'\n'); }); var array = arr.toString().split(',') $("#text").val(array.join("")); }); }); <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <p>Append value when checkbox is checked</p> <textarea rows="4" id="text" style="width: 100%"> </textarea> <div id="checkboxes"> <p><input type="checkbox" value="Item 1"><span>&nbsp;&nbsp; Item 1</span></p> <p><input type="checkbox" value="Item 2"><span>&nbsp;&nbsp; Item 2</span></p> <p><input type="checkbox" value="Item 3"><span>&nbsp;&nbsp; Item 3</span></p> <p><input type="checkbox" value="Item 4"><span>&nbsp;&nbsp; Item 4</span></p> <p><input type="checkbox" value="Item 5"><span>&nbsp;&nbsp; Item 5</span></p> </div>