Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用jquery从复选框组中获取选中值_Javascript_Jquery_Checkbox - Fatal编程技术网

Javascript 如何使用jquery从复选框组中获取选中值

Javascript 如何使用jquery从复选框组中获取选中值,javascript,jquery,checkbox,Javascript,Jquery,Checkbox,我想获取复选框组中所有复选框的值 <div class="checkbox-group"> <div class="checkbox"> <label for="checkbox-group-1501481486714-preview-0"> <input name="checkbox-group-1501481486714-preview[]" class="" id="checkbox-group-1501481486714-preview-0"

我想获取复选框组中所有复选框的值

 <div class="checkbox-group">
<div class="checkbox">
<label for="checkbox-group-1501481486714-preview-0">
<input name="checkbox-group-1501481486714-preview[]" class="" id="checkbox-group-1501481486714-preview-0" value="physics" type="checkbox" checked="checked">physics</label>
</div>
<div class="checkbox"><label for="checkbox-group-1501481486714-preview-1"><input name="checkbox-group-1501481486714-preview[]" class="" id="checkbox-group-1501481486714-preview-1" value="math" type="checkbox">math</label>
</div>
<div class="checkbox"><label for="checkbox-group-1501481486714-preview-2">
<input name="checkbox-group-1501481486714-preview[]" class="" id="checkbox-group-1501481486714-preview-2" value="chemistry" type="checkbox">chemistry</label>
</div>
</div>
但当我检查任何值时,它会给出这个输出

this is unchecked 
this is unchecked 
this is unchecked
this is checked name: checkbox-group-1500619332922: Value: on
this is checked name: checkbox-group-1500619332922: Value: on
this is checked name: checkbox-group-1500619332922: Value: on

提前感谢

您可以执行以下简单循环:

var values = [];
$('input[type="checkbox"]:checked').each(function(i,v){
  values.push($(v).val());
});
如果只想组成一个组,可以说
.group
,然后将选择器更改为

$('.group input[type="checkbox"]:checked')
演示:

$('input[type=“checkbox”]”)。更改(函数(){
$('.checkbox group')。每个(函数(){
var值=[];
$(this).find('input[type=“checkbox”]:checked')。每个(函数(i,v){
value.push($(v).val());
});
console.log(值);
})
})

选择1
选项
2.
选择1
选项
2.

您可以在下面的演示代码中尝试获取选定值

var demo = $("input[type="checkbox"]:checked").map(function() { return $(this).val(); }).get();

你可以试试。

不行。当我选中一个全部返回“开”时出现同样的问题,但如果不选中全部则不返回任何内容。如果未选中它们,您希望返回什么?如果选中任何一个,则不返回任何内容,但希望获取其值,即“phy”、“math”或“phy”.@Bashirahmad如果选中复选框,答案应该在数组中获得这些值是的,我想要相同的结果,但这里的结果正好相反。如果我选中一个循环,则循环运行三次,如果我选中两个循环,则循环运行相同的三次。我认为这里的“输入[type=“checkbox”]:选中”有问题。您可以使用
此.value
。了解您的DOM元素属性。
var demo = $("input[type="checkbox"]:checked").map(function() { return $(this).val(); }).get();