使用jQuery选择复选框组的值 [英] Select values of checkbox group with jQuery
问题描述
我正在使用Zend_Form输出一组复选框:
I'm using Zend_Form to output a set group of checkboxes:
<label style="white-space: nowrap;"><input type="checkbox" name="user_group[]" id="user_group-20" value="20">This Group</label>
使用普通的HTTP Post,这些值作为数组传递,但是当我有点难过时如何使用jQuery获取所有值。我想我可以使用以下方式选择组:
With a normal HTTP Post these values are passed as an array, but when I'm somewhat stumped on how to grab all the values using jQuery. I figured I can select the group using:
$("input[@name='user_group[]']").val()
但这只是抓住列表中第一个复选框的值,无论它是否是检查没有。任何想法?
but that just grabs the value of the first checkbox in the list regardless of if it is checked of not. Any ideas?
推荐答案
您可以使用选中的选择器仅抓取所选的(无需知道计数或自己迭代):
You could use the checked selector to grab only the selected ones (negating the need to know the count or to iterate over them all yourself):
$("input[name='user_group[]']:checked")
使用这些选中的项目,您可以创建这些值的集合或对集合执行某些操作:
With those checked items, you can either create a collection of those values or do something to the collection:
var values = new Array();
$.each($("input[name='user_group[]']:checked"), function() {
values.push($(this).val());
// or you can do something to the actual checked checkboxes by working directly with 'this'
// something like $(this).hide() (only something useful, probably) :P
});
这篇关于使用jQuery选择复选框组的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!