JQUERY:获取已选中和未选中复选框的 ID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6901242/
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-08-26 21:31:37  来源:igfitidea点击:

JQUERY: get the id's of the checked and not checked checkBox

jqueryarrayscheckbox

提问by jayAnn

I have this not so many checkboxes:

我没有这么多复选框:

<input type="checkbox" id="mango" value="mango" /> <label>MANGO</label><br>
<input type="checkbox" id="santol" value="santol" /> <label>SANTOL</label><br>
<input type="checkbox" id="guava" value="guava" /> <label>GUAVA</label><br>
<input type="checkbox" id="lomboy" value="lomboy" /> <label>LOMBOY</label><br>
<input type="checkbox" id="apple" value="apple" /> <label>APPLE</label><br>
<input type="checkbox" id="orange" value="orange" /> <label>ORANGE</label><br>
 ...................<>

Now, what I'm trying to do is get all the ID's of check boxes with check, and no check and put in an array. Something like this:

现在,我正在尝试做的是获取复选框的所有 ID,并且不选中并放入数组中。像这样的东西:

"fruitsGranted":["apple","lomboy","orange"]; //check is true
"fruitsDenied":["mango","santol","guava"];  //check false

can please someone show me how to do it.? thanks.

请有人告诉我该怎么做。?谢谢。

回答by James Montagne

var someObj={};
someObj.fruitsGranted=[];
someObj.fruitsDenied=[];

$("input:checkbox").each(function(){
    var $this = $(this);

    if($this.is(":checked")){
        someObj.fruitsGranted.push($this.attr("id"));
    }else{
        someObj.fruitsDenied.push($this.attr("id"));
    }
});

http://jsfiddle.net/UqrYJ/

http://jsfiddle.net/UqrYJ/

回答by Mo Valipour

I think I got a shorter version here:

我想我在这里得到了一个较短的版本:

var idSelector = function() { return this.id; };
var fruitsGranted = $(":checkbox:checked").map(idSelector).get();
var fruitsDenied = $(":checkbox:not(:checked)").map(idSelector).get();

You can see it in action here: http://jsfiddle.net/XPMjK/3/

你可以在这里看到它的实际效果:http: //jsfiddle.net/XPMjK/3/

回答by John Kalberer

I would do this something like this:

我会这样做:

var all, checked, notChecked;
all = $("input:checkbox");
checked = all.filter(":checked");
notChecked = all.not(":checked)");

After that you can use jQuery.map to get the ids of each collection.

之后,您可以使用 jQuery.map 来获取每个集合的 id。

var checkedIds = checked.map(function() {
    return this.id;
});

var notCheckedIds = notChecked.map(function() {
    return this.id;
});

回答by karim79

This can be done using .map, though in this case it is not really different from using .each:

这可以通过 using 来完成.map,但在这种情况下它与 using 并没有真正的不同.each

$(":checkbox").change(function() {
    var notChecked = [], checked = [];
    $(":checkbox").map(function() {
        this.checked ? checked.push(this.id) : notChecked.push(this.id);
    });
    alert("checked: " + checked);
    alert("not checked: " + notChecked);
});

You can test it here.

你可以在这里测试。