Javascript ($(';。类名';).is(";:checked";)从不为真

Javascript ($(';。类名';).is(";:checked";)从不为真,javascript,php,html,Javascript,Php,Html,我使用循环放置了一些复选框。以下是生成循环的PHP代码: <form class="small-box-footer" style="text-align:left;padding:10px;" method="post" name="nameHere"> <?php while ($row39 = mysql_fetch_array($result)) { $Referrer_ID = $row39['Subject_ID'];

我使用循环放置了一些复选框。以下是生成循环的PHP代码:

<form class="small-box-footer" style="text-align:left;padding:10px;"  method="post" name="nameHere">
    <?php
    while ($row39 = mysql_fetch_array($result)) {
        $Referrer_ID = $row39['Subject_ID'];
        $Referrer_Name = $row39['Subject_Name'];
    ?>
        <input type="checkbox" class="subject-selected"  name="subject" value="<?= $Referrer_ID ?>"> <?= $Referrer_Name ?><?= $Referrer_ID ?><br />        
    <?php
    }
    ?>
</form> 

GCSE数学2
GCSE英语3
GCSE科学4
GCSE Art5
GCSE社会学6
OCR国民ICT8
OCR国民体育9
OCR国民商业研究10
入门科学11
功能技能英语12
功能技能数学13
ESOL14
工作生活的准备15
尝试一下

// loop only for checked checkboxes
$('.subject-selected:checked').each(function(){
    a=$(this).next('a'); // get the next anchor element
    a.length && a.attr( "href", '?module=module_progress_report&Subject='+ this.value+ '&Centre_Selected_ID='+ encodeURIComponent(a.attr('data-centre')) + '&Class_Selected_Year='+ encodeURIComponent(a.attr('data-year')) + '&Class_Selected_All='+ encodeURIComponent(a.attr('data-all-centre')) +'&StartDate='+$('#report_date_start').val()+'&EndDate=18/12/2016');
 });
如@Fahad评论所述

// loop for all checkboxes
$('.subject-selected').each(function(){
    a=$(this).next('a'); // get the next anchor element
    if(this.checked){
       a.length && a.attr( "href", '?module=module_progress_report&Subject='+ this.value+ '&Centre_Selected_ID='+ encodeURIComponent(a.attr('data-centre')) + '&Class_Selected_Year='+ encodeURIComponent(a.attr('data-year')) + '&Class_Selected_All='+ encodeURIComponent(a.attr('data-all-centre')) +'&StartDate='+$('#report_date_start').val()+'&EndDate=18/12/2016');
    } else {
        // not checked code here
    }
 });
为锚定标记循环更新,每次仅获取
2
的原因是因为您使用的是
$('.subject selected')。val()
和jquery将返回第一个复选框元素。因此,您需要使用锚定标记索引来获取其等价的复选框值

$( ".centre-selection" ).each(function(index) {
     $( this ).attr( "href", '?module=module_progress_report&Subject='+ 
       $('.subject-selected:eq('+index+')').val()+ ....);
});
试试看

// loop only for checked checkboxes
$('.subject-selected:checked').each(function(){
    a=$(this).next('a'); // get the next anchor element
    a.length && a.attr( "href", '?module=module_progress_report&Subject='+ this.value+ '&Centre_Selected_ID='+ encodeURIComponent(a.attr('data-centre')) + '&Class_Selected_Year='+ encodeURIComponent(a.attr('data-year')) + '&Class_Selected_All='+ encodeURIComponent(a.attr('data-all-centre')) +'&StartDate='+$('#report_date_start').val()+'&EndDate=18/12/2016');
 });
如@Fahad评论所述

// loop for all checkboxes
$('.subject-selected').each(function(){
    a=$(this).next('a'); // get the next anchor element
    if(this.checked){
       a.length && a.attr( "href", '?module=module_progress_report&Subject='+ this.value+ '&Centre_Selected_ID='+ encodeURIComponent(a.attr('data-centre')) + '&Class_Selected_Year='+ encodeURIComponent(a.attr('data-year')) + '&Class_Selected_All='+ encodeURIComponent(a.attr('data-all-centre')) +'&StartDate='+$('#report_date_start').val()+'&EndDate=18/12/2016');
    } else {
        // not checked code here
    }
 });
为锚定标记循环更新,每次仅获取
2
的原因是因为您使用的是
$('.subject selected')。val()
和jquery将返回第一个复选框元素。因此,您需要使用锚定标记索引来获取其等价的复选框值

$( ".centre-selection" ).each(function(index) {
     $( this ).attr( "href", '?module=module_progress_report&Subject='+ 
       $('.subject-selected:eq('+index+')').val()+ ....);
});

您必须循环浏览所有选定的
主题
,如下所示:

$('.subject-selected').each(function(){
    if ($(this).is(":checked")) {
        $(this).attr("href", '?module=module_progress_report&Subject=' + $("input[type='checkbox']").val() + '&Centre_Selected_ID=' + encodeURIComponent($(this).attr('data-centre')) + '&Class_Selected_Year=' + encodeURIComponent($(this).attr('data-year')) + '&Class_Selected_All=' + encodeURIComponent($(this).attr('data-all-centre')) + '&StartDate=' + $('#report_date_start').val() + '&EndDate=18/12/2016');
    }
});

您必须循环浏览所有选定的
主题
,如下所示:

$('.subject-selected').each(function(){
    if ($(this).is(":checked")) {
        $(this).attr("href", '?module=module_progress_report&Subject=' + $("input[type='checkbox']").val() + '&Centre_Selected_ID=' + encodeURIComponent($(this).attr('data-centre')) + '&Class_Selected_Year=' + encodeURIComponent($(this).attr('data-year')) + '&Class_Selected_All=' + encodeURIComponent($(this).attr('data-all-centre')) + '&StartDate=' + $('#report_date_start').val() + '&EndDate=18/12/2016');
    }
});
$(“.subject selected”).change(函数(){
如果(选中此项){
//做事
}
});
$(“.subject selected”).change(函数(){
如果(选中此项){
//做事
}

});您需要在每个“输入”中添加一个事件侦听器来捕获事件(单击、更改或其他)

尝试:


您需要在每个“输入”中添加一个事件侦听器来捕获事件(单击、更改或其他)

尝试:



请格式化脚本…请使用codepen之类的工具,这很简单,也很有帮助。可能是您希望获得更改的复选框。
$('.subject selected')。每个(…..)
,以及这个。checked…
$('.subject selected')
将选择您的所有复选框。这就是你想要的吗?请格式化脚本…请使用像codepen这样的工具,它很简单而且帮助很大。可能是你想要得到你更改的复选框。
$('.subject selected')。每个(…)
和这个。选中的…
$('.subject selected')
将选中你所有的复选框。这就是你想要的吗?我不想只循环复选框。我希望每个结果都是一个链接,但我想首先检查是否存储了已检查的值。仍然没有生成链接。问题不在这里。真正的问题是复选框的值没有被选中。如果它被选中,我会直接把每一个项目作为一个链接。不需要if语句。那么,您希望如何填充
$(this.attr('data-all-center')
$(this.attr('data-year')
$(this.attr('data-center')
)的值呢。您还可以应用“单击事件”复选框,以便动态更改链接。复选框不同,生成链接的项目也不同。我不想只循环复选框。我希望每个结果都是一个链接,但我想首先检查是否存储了已检查的值。仍然没有生成链接。问题不在这里。真正的问题是复选框的值没有被选中。如果它被选中,我会直接把每一个项目作为一个链接。不需要if语句。那么,您希望如何填充
$(this.attr('data-all-center')
$(this.attr('data-year')
$(this.attr('data-center')
)的值呢。您还可以对复选框应用单击事件,以便动态更改链接。复选框不同,生成链接的项目也不同。从逻辑和语法上看,值未定义且不正确
$(this).attr('data-year')
$(this).attr('data-all-center')
$('input[type='checkbox']”).val()
。如果您对SO给出任何答案,请尝试更正。从逻辑和语法上看,这些值未定义且不正确
$(this).attr('data-year')
$(this).attr('data-all-center')
$([input[type='checkbox'])。val()
。如果您对SO给出任何答案,请尝试更正。