[英]how to get checked values from a checkbox-group using jquery
I want to get the values of all the checked checkboxes in the checkbox-group. 我想获取复选框组中所有选中的复选框的值。
<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>
Following is script code 以下是脚本代码
document.getElementById('btn').addEventListener('click', function() {
$(document).ready(function(){
var x = $(".userform").find(":input");
$.each(x, function(i, field){
if(field.type == "text")
{
$("#results").append("name: "+field.name + ": " +"Value: "+ field.value +"<br>");
}
else if(field.type == "checkbox")
{
var result = $('input[type="checkbox"]:checked');
if(result.length > 0)
{
$("#results").append("this is checked "+"name: "+field.name + ": " +"Value: "+ field.value +"<br>");
}
else{
$("#results").append("this is unchecked");
}
}
});
});
});
When I leave all uncheck then it gives this output 当我全部取消选中时,它将给出此输出
this is unchecked
this is unchecked
this is unchecked
but when I check any it gives this output 但是当我检查任何它给出此输出
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
Thanks in Advance 提前致谢
You can do this simple loop: 您可以执行以下简单循环:
var values = [];
$('input[type="checkbox"]:checked').each(function(i,v){
values.push($(v).val());
});
if you want only form a group lets say .group
then change the selector to 如果只想组成一个小组,可以说
.group
然后将选择器更改为
$('.group input[type="checkbox"]:checked')
demo: 演示:
$('input[type="checkbox"]').change(function() { $('.checkbox-group').each(function() { var values = []; $(this).find('input[type="checkbox"]:checked').each(function(i, v) { values.push($(v).val()); }); console.log(values); }) })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <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="option-1" type="checkbox" checked="checked">Option 1</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="option-2" type="checkbox">Option 2</label> </div> </div> <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="option-1 group-2" type="checkbox" checked="checked">Option 1</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="option-2 group-2" type="checkbox">Option 2</label> </div> </div>
You can try below demo code for get selected value. 您可以尝试在下面的演示代码中获取所选值。
var demo = $("input[type="checkbox"]:checked").map(function() { return $(this).val(); }).get();
you can try. 你可以试试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.