javascript 获取使用 jquery 选择的复选框的 id

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19944421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 17:18:47  来源:igfitidea点击:

Getting id of checkboxes selected using jquery

javascriptjquerycheckbox

提问by marchemike

I'm trying to get the id's of the checkboxes with the class file-selection-id. The checkbox is inside a for loop which is why I have placed variables on the id's and names.

我正在尝试使用 class 获取复选框的 ID file-selection-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; ?>">

What I want is that when I click a button, it would get all of the id's of the selected checkboxes.

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

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

  });
     });

But I don't know how to get the Id's of all the checkboxes that were on the file-selection-id class that I need? What could be ways on how I could call the ID's of the checkboxes(for example if I had 4 checkboxes with 2 selections)?

但我不知道如何获取我需要的文件选择 ID 类中所有复选框的 ID?我可以如何调用复选框的 ID(例如,如果我有 4 个带有 2 个选项的复选框)?

回答by Tushar Gupta - curioustushar

Use .map

使用.map

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



如果你想要ididcheckedchecked的复选框

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()。筛选()