Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/384.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.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 获取所有选中复选框的列表_Javascript_Jquery_Checkbox - Fatal编程技术网

Javascript 获取所有选中复选框的列表

Javascript 获取所有选中复选框的列表,javascript,jquery,checkbox,Javascript,Jquery,Checkbox,我想确定选中了哪些复选框,因此我尝试了以下代码: $('.feature input[type="checkbox"').serialize(); 这是我的HTML的外观: <div class="feature"> <h2>Features</h2> <label><input class="custom_css" checked="" type="checkbox" name="feature[]"> Custom CSS

我想确定选中了哪些复选框,因此我尝试了以下代码:

$('.feature input[type="checkbox"').serialize();
这是我的HTML的外观:

<div class="feature">
  <h2>Features</h2>
  <label><input class="custom_css" checked="" type="checkbox" name="feature[]"> Custom CSS (style.css)</label>
  <label><input class="custom_js" checked="" type="checkbox" name="feature[]"> Custom Javascript (script.js)</label>
  <label><input class="modernizr" type="checkbox" name="feature[]"> Modernizr</label>
  <label><input class="google_maps" type="checkbox" name="feature[]"> Google Maps</label>
  <label><input class="custom_api" type="checkbox" name="feature[]"> Custom API</label>
  <label><input class="font_awesome" type="checkbox" name="feature[]"> Font Awesome</label>
</div>

特征
自定义CSS(style.CSS)
自定义Javascript(script.js)
现代化者
谷歌地图
自定义API
象形异体字
这是我得到的输出:

数组(1){[“var_sent_via_ajax”]=>字符串(67) “功能%5B%5D=打开&功能%5B%5D=打开&功能%5B%5D=打开&功能%5B%5D=打开” }

现在我怎么知道哪一个已经检查过了?符号%5B%5D是什么意思

这将显示选中复选框的数量

$(".feature input[type='checkbox']:checked")

这将为您提供选中的复选框(对象)

您可以按名称获取它们,然后逐个选中

    var elems= document.querySelectorAll('[name=feature[]')
                for (var i=0;i<elems.length;i++)
                {
                    var isChecked =elems[i].checked;
                    console.log(isChecked);
                }
var elems=document.querySelectorAll(“[name=feature[]”)

对于(var i=0;i关于:
%5B
%5D

答:它们只是
[
]
的原始HTTP编码值(序列化函数的结果)。
当服务器解析它时,它将其转换为
[]
,并将其发送到将被视为数组的应用程序。

关于为什么会出现这种情况:
feature%5B%5D=on&feature%5B%5D=on…
string
回答:您忘记给每个复选框指定一个值参数,那么它们将是这样的:
feature%5B%5D=custom_css&feature%5B%5D=custom_js…

我已经编写了解决方案。
以这个工作示例为例,在服务器端应用程序上像字符串一样处理请求的“feature”参数,并将其收缩
(php:
$features=explode(',',$\u POST['features']

$(函数(){
$('#getFeatures')。单击(函数(){
var特征=[];
$('.feature input[type=“checkbox”]:checked')。每个(函数(){
features.push($(this.val());
});
$('#selectedFeatures').html(features.join(',');
});
});

特征
自定义CSS(style.CSS)
自定义Javascript(script.js)
现代化者
谷歌地图
自定义API
象形异体字
获取特征

您可以通过以下示例获得答案:

$('input[name="feature"]:checked').each(function() {
   console.log(this.value);
});

相关?您可以使用
:checked
$('.feature input:checked')
非常感谢!这绝对是一个很好的答案,对我帮助很大!
$('input[name="feature"]:checked').each(function() {
   console.log(this.value);
});