【问题标题】:Get list of all checkboxes with checked and unchecked status获取具有选中和未选中状态的所有复选框的列表
【发布时间】:2015-06-02 04:02:10
【问题描述】:

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

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

这行得通,我可以获得所有选中的复选框,但我真正想要的是获得所有复选框列表,这些复选框已选中,哪些未选中。我该如何修改此代码?

【问题讨论】:

  • 如果你得到所有选中和未选中的元素,你怎么知道哪些被选中
  • var checked = $('input[name="ringIds[]"]').serialize();

标签: javascript php jquery html


【解决方案1】:

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.

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

您可能希望使用.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('&');

【讨论】:

    【解决方案2】:

    这是您要查找的代码:

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

    【讨论】:

    • ringIds%5B%5D=12&ringIds%5B%5D=15&ringIds%5B%5D=19 这就是我得到的,显然它只显示那些被检查的。即使有序列化
    • 我想获取已选中和未选中的环列表
    猜你喜欢
    • 2016-11-06
    • 1970-01-01
    • 1970-01-01
    • 2019-06-08
    • 1970-01-01
    • 1970-01-01
    • 2019-10-17
    • 1970-01-01
    • 2018-04-25
    相关资源
    最近更新 更多