获取状态为已选中和未选中的所有复选框的列表


Get list of all checkboxes with checked and unchecked status

我有以下代码,显示图像及其复选框。是启用还是禁用

 if($ring["status"] != '1') 
                                        echo '<td><input type="checkbox" name="ringIds[]" value="'.$ring["id"].'">'
                                    . '<input type="image" onClick="openGalleryRing('''.$ring['imagePath'].''', '''.$ring['id'].''');" src="http://thevowapp.com/iphoneapp/vowstore/rings/'.  $ring['imagePath'] .'" name="checked" value="' . $ring['id'].'" data-my-info="'. $ring['ringSetName'] .'" style="width:200px; height:200px; margin-left: 10px;"></td>';
                                    else                                         
                                         echo '<td><input type="checkbox" checked="checked"  name="ringIds[]" value="'.$ring["id"].'">'
                                    . '<input type="image" onClick="openGalleryRing('''.$ring['imagePath'].''', '''.$ring['id'].''');" src="http://thevowapp.com/iphoneapp/vowstore/rings/'.  $ring['imagePath'] .'" name="checked" value="' . $ring['id'].'" data-my-info="'. $ring['ringSetName'] .'" style="width:200px; height:200px; margin-left: 10px;"></td>';

在我的javascript中,我使用类似

的东西
$('.updateBtn').on('click', function()
        {
             var checked = $('input[name="ringIds[]"]:checked').serialize(); 
             if(checked !== '')
                 window.location.href = 'actions.php?j=24&' + checked;
             else
                 alert('No Rings are selected');
    }); 

这是有效的,我可以得到所有选中的复选框,但我实际上想要的是得到所有的复选框列表,哪些是选中的,哪些不是。我如何修改这段代码?

注意:只有"成功的控件"被序列化为字符串。没有提交按钮值被序列化,因为表单没有提交使用按钮。元素中包含的表单元素的值序列化字符串时,元素必须具有name属性。从的值复选框和单选按钮(输入类型为"radio"或"checkbox")仅在选中时才包含。数据从文件选择元素

因此,无论您是否包含伪选择器:checked, .serialize()将只返回已选中的复选框。

您可能需要使用.each().map()来获取未选中的复选框。

var unchecked = "";
$('input[name="ringIds[]"]').not(':checked').each(function() {
    unchecked += (unchecked.length ? '&' + '') + this.name + '=' + this.value;
});

或:

var unchecked = $('input[name="ringIds[]"]').not(':checked').map(function(i,v) {
    return this.name + '=' + this.value;
})
.get().join('&');

这是你要找的代码:

$('.updateBtn').on('click', function()
{
    var check_boxes = $('input[name="ringIds[]"]').serialize(); 
    // Try below line to see all checkboxes
    console.log(check_boxes);
});