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

时间:2015-06-02 04:02:11

标签: javascript php jquery html

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

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

这样,我可以获得所有选中的复选框,但我真正想要的是获取所有复选框的复选框,这些复选框已经过检查,哪些不是。我该如何修改这段代码?

2 个答案:

答案 0 :(得分:3)

  

Note: Only "successful controls" are serialized to the string. No submit button value is serialized since the form was not submitted using a button. For a form element's value to be included in the serialized string, the element must have a name attribute. Values from checkboxes and radio buttons (inputs of type "radio" or "checkbox") are included only if they are checked. Data from file select elements is not serialized.

因此span[id^="step-"].done:not(-- with lots of combinations of last child on parent as well as child --)将仅返回选中的复选框,无论您是否包含伪选择器.serialize()

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

.map()

或者:

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

答案 1 :(得分:1)

以下是您要查找的代码:

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

});