我有这些复选框:

<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());
    });

其他回答

使用Jquery

你只需要添加类到每个输入,我有添加类“源”,你当然可以改变它

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

<script type="text/javascript">
$(document).ready(function() {
    var selected_value = []; // initialize empty array 
    $(".source:checked").each(function(){
        selected_value.push($(this).val());
    });
    console.log(selected_value); //Press F12 to see all selected values
});
</script>

你可以尝试这样做:

$('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,

ES6版本:

const values = Array
  .from(document.querySelectorAll('input[type="checkbox"]'))
  .filter((checkbox) => checkbox.checked)
  .map((checkbox) => checkbox.value);

function getCheckedValues() { return Array.from(document.querySelectorAll('input[type="checkbox"]')) .filter((checkbox) => checkbox.checked) .map((checkbox) => checkbox.value); } const resultEl = document.getElementById('result'); document.getElementById('showResult').addEventListener('click', () => { resultEl.innerHTML = getCheckedValues(); }); <input type="checkbox" name="type" value="1" />1 <input type="checkbox" name="type" value="2" />2 <input type="checkbox" name="type" value="3" />3 <input type="checkbox" name="type" value="4" />4 <input type="checkbox" name="type" value="5" />5 <br><br> <button id="showResult">Show checked values</button> <br><br> <div id="result"></div>

$(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>

var chk_arr =  document.getElementsByName("chkRights[]");
var chklength = chk_arr.length;             

for(k=0;k< chklength;k++)
{
    chk_arr[k].checked = false;
}