jQuery获取所有选中值不起作用

jQuery获取所有选中值不起作用,jquery,html,Jquery,Html,任何人都可以帮助我为什么它返回一个空数组。先谢谢你。您可以在上查看代码 代码示例: 函数搜索(){ var-pettype=[]; $(“.pettype:checked”).each(函数(){ 警报($(this.val()); push($(this.val()); }) 警报(pettype); } 事实上,我认为你犯了一个很小的错误。。。您有$(“.pettype:checked”),但它必须是$(“.pettype:checked”),因为否则jQuery正在搜索一个选中的元素,该

任何人都可以帮助我为什么它返回一个空数组。先谢谢你。您可以在上查看代码

代码示例:

函数搜索(){
var-pettype=[];
$(“.pettype:checked”).each(函数(){
警报($(this.val());
push($(this.val());
})
警报(pettype);
}

事实上,我认为你犯了一个很小的错误。。。您有
$(“.pettype:checked”)
,但它必须是
$(“.pettype:checked”)
,因为否则jQuery正在搜索一个选中的元素,该元素是pettype的子元素,而不是实际的pettype:)

函数搜索(){
var-pettype=[];
$(“.pettype:checked”).each(函数(){
push($(this.val());
})
警报(pettype);
}

HTML代码:

您只需删除以下行中的多余空间即可:

$(".pettype :checked").each(function(){
___________^
应该是:

$(".pettype:checked").each(function(){
否则,选择器将在
.pettype
元素中搜索选中的元素

希望这有帮助

函数搜索(){
var-pettype=[];
$(“.pettype:checked”).each(函数(){
log($(this.val());
push($(this.val());
})
警报(pettype);
}