简体   繁体   English

如何在两个单独的数组中获取复选框jquery的值,其中一个包含已检查的值,另一个未经检查

[英]How to get values of checkbox jquery in two separate arrays one contaings checked values and other unchecked

Below is the code through which I get the values of checked input boxes: 下面是我获取已检查输入框值的代码:

$('#agent_region').click(function()
         {
           var ids = $('#agent_favourites_form  input:checked').map(function(){
             return this.value;
           }).get();
           $.ajax({
             type: "POST",
             url: "set_agent_favourites.php",
             data:  {map: ids},
             success: function(msg){
               $.jnotify(msg, 3000);
             }
           });
           return false;
         });

I want to get the values of unchecked checkboxes as well and pass it, I don't know how to pass two data: {map : ids}, {map1 : ids1} or something else. 我想获取未选中复选框的值并传递它,我不知道如何传递两个数据: {map : ids}, {map1 : ids1}或其他。 Please help. 请帮忙。

You can use input:not(:checked) and just add it into your data object: 您可以使用input:not(:checked)并将其添加到数据对象中:

$('#agent_region').click(function()
         {
           var ids = $('#agent_favourites_form  input:checked')
                       .map(function(){
                         return this.value;
                       }).get();
           var nonids = $('#agent_favourites_form  input:not(:checked)')
                          .map(function(){
                            return this.value;
                          }).get();
           $.ajax({
             type: "POST",
             url: "set_agent_favourites.php",
             data:  {map: ids, unmap: nonids},
             success: function(msg){
               $.jnotify(msg, 3000);
             }
           });
           return false;
         });

Use the jQuery checked selector 使用jQuery检查的选择器

See: http://api.jquery.com/checked-selector/ 请参阅: http//api.jquery.com/checked-selector/

$(function() {
    var checked = {};
    var unchecked = {};
    $('input[type=checkbox]').each(function() {
        if ($(this).is(':checked')) {
            checked[$(this).attr('name')] = $(this).val();
        } else {
            unchecked[$(this).attr('name')] = $(this).val();
        }
    });

    // Only works with FireBug enabled
    console.debug(checked);
    console.debug(unchecked);
});

Demo: http://jsfiddle.net/x3TcD/ 演示: http//jsfiddle.net/x3TcD/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM