2013-04-23 131 views
97

我試圖獲取當前選中的所有複選框的值並將它們存儲到數組中。這是我的代碼到目前爲止:jQuery將選中的複選框的值存入數組

$("#merge_button").click(function(event){ 
    event.preventDefault(); 
    var searchIDs = $("#find-table input:checkbox:checked").map(function(){ 
     return $(this).val(); 
    }); 
    console.log(searchIDs); 
    }); 

但是,這種產出超過我的需要。我不僅獲得了價值觀,還獲得了一些我不想要的東西。

[ 「51729b62c9f2673e4c000004」, 「517299e7c9f26782a7000003」, 「51729975c9f267f3b5000002」,prevObject:jQuery.fn.jQuery.init [3], 上下文:文檔,jquery的: 「1.9.1」,構造:功能,初始化: 函數...]

我想只是ID的(在這種情況下的前3項)。

通過使用$.each和推值到一個數組我得到所需的輸出:

$("#find-table input:checkbox:checked").each(function(){ myArray.push($(this).val()); }) 

[ 「51729b62c9f2673e4c000004」, 「517299e7c9f26782a7000003」, 「51729975c9f267f3b5000002」]

但是我d喜歡使用$.map,因爲它爲我節省了一行代碼並且更漂亮。

感謝

回答

200

呼叫.get()把所產生的jQuery對象成爲真正的陣列。

$("#merge_button").click(function(event){ 
    event.preventDefault(); 
    var searchIDs = $("#find-table input:checkbox:checked").map(function(){ 
     return $(this).val(); 
    }).get(); // <---- 
    console.log(searchIDs); 
}); 

the documentation

由於返回值是一個jQuery對象,其中包含一個數組,這是很常見的調用獲得()的結果與基本陣列工作。

+1

我期待從'$ .map'真正的數組。感謝解決方案,它的工作原理。 – 2013-04-23 15:16:54

+0

非常感謝你,我需要這個ma Ticketcenter – Steven 2013-06-21 17:50:22

+2

我沒有得到這件事的事情是爲什麼'.get()'是需要的,當函數返回'.val()'從每個項目jQuery集合。這是因爲'map'在將它傳遞給'searchIDs'之前將它變回jQuery對象? – iconoclast 2013-10-16 15:17:45

14

您需要添加.toArray().map()函數結束

$("#merge_button").click(function(event){ 
    event.preventDefault(); 
    var searchIDs = $("#find-table input:checkbox:checked").map(function(){ 
     return $(this).val(); 
    }).toArray(); 
    console.log(searchIDs); 
}); 

演示:在最後http://jsfiddle.net/sZQtL/

9

我重構了你的代碼,並相信我提供了你正在尋找的解決方案。基本上不是將searchIDs設置爲.map()的結果,而是將值推入數組中。

$("#merge_button").click(function(event){ 
    event.preventDefault(); 

    var searchIDs = []; 

    $("#find-table input:checkbox:checked").map(function(){ 
    searchIDs.push($(this).val()); 
    }); 

    console.log(searchIDs); 
}); 

我創建了一個運行代碼的fiddle

+0

這基本上是OP在他/她的問題中的正確結果...... – 2013-04-23 13:54:33

18

DEMO:http://jsfiddle.net/PBhHK/

$(document).ready(function(){ 

    var searchIDs = $('input:checked').map(function(){ 

     return $(this).val(); 

    }); 
    console.log(searchIDs.get()); 

}); 

只需調用get()方法,你就會有你的陣列,因爲它是寫在規格:http://api.jquery.com/map/

$(':checkbox').map(function() { 
     return this.id; 
    }).get().join(); 
0

不要使用 「每個」。它用於相同元素的操作和更改。使用「map」從元素體中提取數據並在其他地方使用它。

+1

會很好,有這樣的示例代碼 – 2017-10-18 21:19:47

+0

jQuery文檔對大多數所有內容都有很好的示例,包括'map' – jinglesthula 2017-11-21 16:23:44

0

需要將HTML中作爲數組命名的表單元素作爲javascript對象中的數組,就好像表單實際上已提交一樣。

如果有多個複選框如窗體:

<input name='breath[0]' type='checkbox' value='presence0'/> 
<input name='breath[1]' type='checkbox' value='presence1'/> 
<input name='breath[2]' type='checkbox' value='presence2'/> 
<input name='serenity' type='text' value='Is within the breath.'/> 
... 

結果是與對象:

data = { 
    'breath':['presence0','presence1','presence2'], 
    'serenity':'Is within the breath.' 
} 


var $form = $(this), 
    data = {}; 

$form.find("input").map(function() 
{ 
    var $el = $(this), 
     name = $el.attr("name"); 

    if (/radio|checkbox/i.test($el.attr('type')) && !$el.prop('checked'))return; 

    if(name.indexOf('[') > -1) 
    { 
     var name_ar = name.split(']').join('').split('['), 
      name = name_ar[0], 
      index = name_ar[1]; 

     data[name] = data[name] || []; 
     data[name][index] = $el.val(); 
    } 
    else data[name] = $el.val(); 
}); 

而且還有噸答案在這裏這有助於提高我的代碼,但他們要麼太複雜,要麼完全沒有做我想要的:Convert form data to JavaScript object with jQuery

工作,但可以改進:只適用於一維數組和re暗瘡指數可能不是連續的。數組的length屬性返回下一個索引號作爲數組的長度,而不是實際的長度。

希望這有助於。合十!

3
 var ids = []; 

$('input[id="find-table"]:checked').each(function() { 
    ids.push(this.value); 
}); 

這一個爲我工作!