[英]jquery How to access only checked checkbox?
我有一个包含7 checkboxes
的div,我编写了以下代码,用于only the checked checkbox
访问到array icons
var icons = $("div").find("input[type=checkbox]").each(function () {
if($(this).attr("checked") == "checked") return this; });
alert(icons.length);
但它始终会提示7。有人可以告诉我问题吗?
var icons = $("div").find("input[type=checkbox]:checked");
var icons= $("div").find("input:checked").length;
alert(icons);
最简单的方法是将一个类分配给目标输入并使用选中的选择器 ,请参见下面的示例:
var icons = $('.box:checked');
您可以使用$.map
,
$(document).ready(function() {
var checks = $.map($("div").find('input[type="checkbox"]:checked'),function(arr){
return arr;
})
console.log(checks.length);
})
或你的方法
$(document).ready(function() {
var checks = $.each($("div").find('input[type="checkbox"]:checked'),function(){
return this;
})
console.log(checks.length);
})
Hiya 演示 http://jsfiddle.net/KzMqB/
您可能只需要使用:checked
检查一次,它将为您提供所需的内容。
好的阅读: http : //api.jquery.com/checked-selector/
希望这可以帮助!
码
var icons = $("div").find("input[type=checkbox]:checked").each(function() {
alert("ID of checked checkbox = " + $(this).attr("id"));
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.