Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.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 检查span中的复选框是否已选中_Javascript_Jquery_Dom_Checkbox_Each - Fatal编程技术网

Javascript 检查span中的复选框是否已选中

Javascript 检查span中的复选框是否已选中,javascript,jquery,dom,checkbox,each,Javascript,Jquery,Dom,Checkbox,Each,我有一个小的.each()循环来检查span是否可见,然后我想检查span标记中是否选中了复选框 功能是 function processReports(){ $("#processedReports").html(''); var repName = $("#reviewType").val(); var divHTML = "<table style='width: 300px;'><tr><td style='width:30%;

我有一个小的.each()循环来检查span是否可见,然后我想检查span标记中是否选中了复选框

功能是

function processReports(){
    $("#processedReports").html('');
    var repName = $("#reviewType").val();   
    var divHTML = "<table style='width: 300px;'><tr><td style='width:30%; text-align:left; font-weight:bold;'>Report</td><td style='font-weight:bold; width:50%; text-align:right; padding-right:4px;'>Date/Time Run</td><td style='font-weight:bold;'>Progress</td></tr>";
    var d = new Date();
    var strDate =  (d.getMonth()+1) + "/" + d.getDate() + "/" + d.getFullYear();
    var strTime = d.toLocaleTimeString();
    $("#reportChecks span:visible").each(function(){
        var reportName = $(this).attr("reportType");
        **if($('input.checkbox').is(':checked')){**
            divHTML += "<tr><td style='width:30%; text-align:left;' class='" + reportName +"' advID='" + $(this).text() + "'>" + reportName + "</td><td style='width:50%; text-align:right; padding-right:4px;'>" + strDate + "," + strTime + "</td><td><a href='/Applications/help/Reports/Prospect_reports/Prospect1ClickReview.pdf' target='_blank'>View</a></td></tr>";
        }
        //alert($(this).text());
    });
    divHTML += "</table>";
    $("#processedReports").prepend(divHTML);
    $("#processedReports").show();
    $("#IndReports").show();
}
函数processReports(){
$(“#processedReports”).html(“”);
var repName=$(“#reviewType”).val();
var divHTML=“ReportDate/Time RunProgress”;
var d=新日期();
变量strDate=(d.getMonth()+1)+“/”+d.getDate()+“/”+d.getFullYear();
var strTime=d.toLocaleTimeString();
$(“#reportChecks span:visible”)。每个(函数(){
var reportName=$(this.attr(“reportType”);
**if($('input.checkbox')。是(':checked')){**
divHTML+=“”+reportName+“”+strDate+“”,“+strTime+”;
}
//警报($(this.text());
});
divHTML+=“”;
$(“#processedReports”).prepend(divHTML);
$(“#processedReports”).show();
$(“#IndReports”).show();
}
基本上,我的if($('input.checkbox').is('checked'){……不起作用。span标记中只有一个复选框,所以我只想知道该元素是否被选中,如果它是附加divHTML的,如果不是,继续循环
谢谢!

您需要搜索当前span对象的子代以获取其中的输入。您可以使用
find()
或在选择器的上下文中传递
。如果使用上下文,它将被转换为find(),因此我更喜欢使用find。我假设。checkbox是您尝试访问的checkbox元素的类

使用find()

如果
复选框
而不是类,则键入

if($(this).find(':checkbox').is(:checked'))
使用上下文

使用以下值进行检查:

在没有赋值的情况下,这将为它的值返回一个布尔值。它也比
.is(':checked')快。

当然,如果只有一个复选框,您可以使用以下方式:

if($(this).find('.checkbox')[0].checked)

这是一种类型,我使用了两种答案的组合,使用.prop而不是.is(),并使用带有复选框类型的find方法,希望我可以接受这两种答案。谢谢!
if($('input.checkbox', this).is(':checked'))
if($(this).find('.checkbox').prop('checked'))
if($(this).find('.checkbox')[0].checked)