[英]How to get value of all selected checkbox by using jQuery?
HTML代码:
<form method="post" id="cate-form">
<ul class="expander-list" id="category">
<li>
<div class="radio" style="padding-left:0px">
<label>
<input type="checkbox" id="all" value="all">
All </label>
</div>
</li>
<li>
<div class="radio" style="padding-left:0px">
<label>
<input type="checkbox" name="filter[]" value="active">
Active </label>
</div>
</li>
<li>
<div class="radio" style="padding-left:0px">
<label>
<input type="checkbox" name="filter[]" value="inactive">
Inactive </label>
</div>
</li>
</ul>
</form>
jQuery代码:
<script>
$(document).ready(function(){
$("#all").click(function() {
$(':checkbox').prop('checked', this.checked);
});
$(":checkbox").change(function(){
var checked = [];
alert("it running");
$('input[name=checkbox] :checked').each(function(){
alert("data is pushing inside checked array");
checked.push($(this).val());
})
})
});
</script>
通过单击所有复选框,可以选择所有复选框。 但是我如何一次获得所有选定复选框的值。我在google上搜索了此查询,但是没有找到任何有用的信息,请立即帮助我解决此问题,在此先感谢。
更改
$('input[name=checkbox] :checked')
至
$('input[type=checkbox]:checked')
因为您的复选框实际上被命名为filter[]
但是它们的类型是checkbox。 您的代码应该可以正常工作
如果您想要数组中的所有值,请尝试以下操作:-
var checkedValues = $('input:checkbox:checked').map(function() {
return this.value;
}).get();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.