获取使用jquery选中的复选框的ID

时间:2013-11-13 03:00:50

标签: javascript jquery checkbox

我正在尝试使用类file-selection-id获取复选框的ID。该复选框位于for循环中,这就是我在id和名称上放置变量的原因。

<input class="file-selection-id" type="checkbox" value="" name="file<?php echo $i; ?>_<?php echo $t; ?>" id="file<?php echo $i; ?>_<?php echo $t; ?>">

我想要的是,当我点击一个按钮时,它会获得所选复选框的所有ID。

$('.move-file-buttons').click( 
function(event) { 
  //??? 
$('input:checkbox.file-selection-id').each(function () {

  });
     });

但我不知道如何获取我需要的file-selection-id类的所有复选框的Id?有什么办法可以调用复选框的ID(例如,如果我有4个带2个选项的复选框)?

1 个答案:

答案 0 :(得分:5)

使用.map

var arr = $('input:checkbox.file-selection-id').map(function () {
    return this.id;
}).get();

<小时/> 如果您需要id checked个复选框

var arr = $('input:checkbox.file-selection-id').filter(':checked').map(function () {
    return this.id;
}).get();

var arr = $('input:checkbox.file-selection-id:checked').map(function () {
    return this.id;
}).get();

<小时/> .filter()

相关问题