所以我有两类复选框。
<input type="checkbox" name="foo" value="val1" />
<input type="checkbox" name="foo" value="val2" />
<input type="checkbox" name="bar" value="val1" />
<input type="checkbox" name="bar" value="val2" />
选中一个框后,我想运行一个将分配两个变量的 jQuery 函数
var foo = //the values of the checked boxes with the foo name
var bar = //the values of the checked boxes with the bar name
所以假设在 foo 组中只有第一个复选框被选中,而在 bar 组中两个都被选中。组值如下
var foo = val1;
var bar = val1,val2;
搜索共享相同名称/类的所有复选框的最佳方法是什么查看它们是否被选中,如果是,则将它们的值添加到字符串中?
最佳答案
你可以尝试类似的东西
var foo = $("input:checkbox[name='foo']:checked").map(function(){
return this.value;
}).get().join(',');
var bar = $("input:checkbox[name='bar']:checked").map(function(){
return this.value;
}).get().join(',');
查看 working demo
关于javascript - 使用 JS 获取复选框值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6489882/