Javascript 如何获取复选框的所有值
我想获取CheckBox的所有值并按id存储它们:Javascript 如何获取复选框的所有值,javascript,Javascript,我想获取CheckBox的所有值并按id存储它们: var jsonObj = $("[type='checkbox']").map(function(i, o) { return { id : o.id, checked : o.checked }; }); var checkboxs = ''; if(jsonObj != 0)
var jsonObj = $("[type='checkbox']").map(function(i, o) {
return { id : o.id, checked : o.checked };
});
var checkboxs = '';
if(jsonObj != 0){
checkboxs = JSON.stringify(jsonObj);
}else{
checkboxs = '' ;
}
复选框的输出
变量:
(www.json.parser.online.fr上的复制/过去输出)
但它会返回一些我根本不需要的值
"length":5,"prevObject":{"0":{"jQuery18305884705366101428":23},"1":{"jQuery18305884705366101428":31},"2":{"jQuery18305884705366101428":39},"3":{"jQuery18305884705366101428":47},"4":{"jQuery18305884705366101428":55},"length":5,"prevObject":{"0":{"location":{"href":"mysite...","origin":"mysite...","protocol":"http:","username":"","password":"","host":"mysite...","hostname":"mysite...","port":"","pathname":"mysite...","search":"","hash":""},"jQuery18305884705366101428":1},"context":{"location":{"href":"mysite...","origin":"mysite...","protocol":"http:","username":"","password":"","host":"mysite...","hostname":"mysite...","port":"","pathname":"mysite.../subs/14988","search":"","hash":""},"jQuery18305884705366101428":1},"length":1},"context":{"location":{"href":"mysite...","origin":"mysite...","protocol":"http:","username":"","password":"","host":"mysite...","hostname":"mysite...","port":"","pathname":"mysite...","search":"","hash":""},"jQuery18305884705366101428":1},"selector":"[type='checkbox']"},"context":{"location":{"href":"mysite...","origin":"mysite...","protocol":"http:","username":"","password":"","host":"mysite...","hostname":"mysite...","port":"","pathname":"mysite...","search":"","hash":""},"jQuery18305884705366101428":1}}
让我一步一步地解释一下整个工作计划。请参见以下片段中的注释:
$(函数(){
//步骤1:创建一个最终对象,该对象存储所有值。
var allChk={};
//步骤2:循环检查所有复选框。
$(“输入:复选框”)。每个(函数(){
//步骤3:对于每个复选框,使用this.id添加索引作为复选框的id,并给出值,无论是否使用this.checked。
allChk[this.id]=this.checked;
});
//您已经用所需的必要信息填充了allChk对象。
});代码>
我认为您没有正确使用map这根本不是问题的目的。他显然希望将ID和选中的布尔标志存储为JSON。如果您将$(this.attr('name')
更改为this.ID
,然后解释您的代码,那么我会。现在,它仍然缺乏解释。“仅使用此代码”是不可接受的。此外,您提供了一个替代解决方案,而不是试图解决OP的真正问题。这是对最初目标的减损,当然是存在的。这就是为什么我不喜欢新手使用jQuery;他们不明白这到底是怎么回事。我已经取消了我的否决票,但下次,不要提出你自己的解决方案,请尝试解决OP的真正问题。
"length":5,"prevObject":{"0":{"jQuery18305884705366101428":23},"1":{"jQuery18305884705366101428":31},"2":{"jQuery18305884705366101428":39},"3":{"jQuery18305884705366101428":47},"4":{"jQuery18305884705366101428":55},"length":5,"prevObject":{"0":{"location":{"href":"mysite...","origin":"mysite...","protocol":"http:","username":"","password":"","host":"mysite...","hostname":"mysite...","port":"","pathname":"mysite...","search":"","hash":""},"jQuery18305884705366101428":1},"context":{"location":{"href":"mysite...","origin":"mysite...","protocol":"http:","username":"","password":"","host":"mysite...","hostname":"mysite...","port":"","pathname":"mysite.../subs/14988","search":"","hash":""},"jQuery18305884705366101428":1},"length":1},"context":{"location":{"href":"mysite...","origin":"mysite...","protocol":"http:","username":"","password":"","host":"mysite...","hostname":"mysite...","port":"","pathname":"mysite...","search":"","hash":""},"jQuery18305884705366101428":1},"selector":"[type='checkbox']"},"context":{"location":{"href":"mysite...","origin":"mysite...","protocol":"http:","username":"","password":"","host":"mysite...","hostname":"mysite...","port":"","pathname":"mysite...","search":"","hash":""},"jQuery18305884705366101428":1}}