我有这些复选框:

<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 idsComenzi = [];

    $('input:checked').each(function(){
        idsComenzi.push($(this).val());
    });

其他回答

你可以尝试这样做:

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

这是一个老问题,但在2022年有一个更好的方法来实现它使用香草JS

我们不需要react或者花哨的框架。

我们只需要处理两个这样的onchange事件:

const types = [{id:1, name:'1'}, {id:2, name:'2'}, {id:3, name:'3'}, {id:4, name:'4'}, {id:5, name:'5'}, {id:6, name:'6'}] const all = document.getElementById('select-all') const summary = document.querySelector('p') let selected = new Set() const onCheck = event => { event.target.checked ? selected.add(event.target.value) : selected.delete(event.target.value) summary.textContent = `[${[...selected].join(', ')} | size: ${selected.size}] types selected.` all.checked = selected.size === types.length } const createCBInput = t => { const ol = document.querySelector('ol') const li = document.createElement('li') const input = document.createElement('input') input.type = 'checkbox' input.id = t.id input.name = 'type' input.value = t.id input.checked = selected.has(t.id) input.onchange = onCheck const label = document.createElement('label') label.htmlFor = t.id label.textContent = t.name li.append(input, label) ol.appendChild(li) } const onSelectAll = event => { const checked = event.target.checked for (const t of types) { const cb = document.getElementById(t.id) cb.checked = checked ? true : selected.has(t.id) const event = new Event('change') cb.dispatchEvent(event) } } all.checked = selected.size === types.length all.onchange = onSelectAll for (const t of types) { createCBInput(t) } ol { list-style-type: none; padding-left: 0; } <ol> <li> <input type="checkbox" id="select-all"> <label for="select-all"><strong>Select all</strong></label> </li> </ol> <p></p>

纯JavaScript,不需要临时变量:

Array.from(document.querySelectorAll("input[type=checkbox][name=type]:checked"), e => e.value);
 var idsComenzi = [];

    $('input:checked').each(function(){
        idsComenzi.push($(this).val());
    });

使用注释if块,以防止添加值已经在数组中,如果你使用按钮点击或其他东西来运行插入

$('#myDiv').change(function() { var values = []; { $('#myDiv :checked').each(function() { //if(values.indexOf($(this).val()) === -1){ values.push($(this).val()); // } }); console.log(values); } }); <div id="myDiv"> <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" /> </div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>