下面的代码片段是从该 PHP 代码生成的 HTML 以及在选中该复选框时创建-6ren">
gpt4 book ai didi

javascript - ($ ('.class-name' ).is (":checked")) 从来不为真

转载 作者:行者123 更新时间:2023-11-28 13:16:33 25 4
gpt4 key购买 nike

我使用循环放置了一些复选框。下面是生成循环的 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>

下面的代码片段是从该 PHP 代码生成的 HTML 以及在选中该复选框时创建链接的 JavaScript 代码;问题是 if 条件没有成立:

   	  $( ".centre-selection" ).each(function() {
$( this ).attr( "href", '?module=module_progress_report&Subject='+ $('.subject-selected').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');


} );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" class="subject-selected" name="subject" value="2"> GCSE Maths2<br />
<input type="checkbox" class="subject-selected" name="subject" value="3"> GCSE English3<br />
<input type="checkbox" class="subject-selected" name="subject" value="4"> GCSE Science4<br />
<input type="checkbox" class="subject-selected" name="subject" value="5"> GCSE Art5<br />
<input type="checkbox" class="subject-selected" name="subject" value="6"> GCSE Sociology6<br />
<input type="checkbox" class="subject-selected" name="subject" value="8"> OCR Nationals ICT8<br />
<input type="checkbox" class="subject-selected" name="subject" value="9"> OCR Nationals Sports9<br />
<input type="checkbox" class="subject-selected" name="subject" value="10"> OCR Nationals Business Studies10<br />
<input type="checkbox" class="subject-selected" name="subject" value="11"> Entry Science11<br />
<input type="checkbox" class="subject-selected" name="subject" value="12"> Functional Skills English12<br />
<input type="checkbox" class="subject-selected" name="subject" value="13"> Functional Skills Maths13<br />
<input type="checkbox" class="subject-selected" name="subject" value="14"> ESOL14<br />
<input type="checkbox" class="subject-selected" name="subject" value="15"> Preparation for Working Life15<br />

编辑:

我认为这个问题没有被正确理解。下面是我编写的第一个 JavaScript,

$( ".centre-selection" ).each(function() {
//$( this ).attr( 'href', '?module=<?=$_REQUEST['module']?>&Subject='+ $( this ).attr('data-subject')+ '&Centre_Selected_ID='+ $( this ).attr('data-centre')+ '&Class_Selected_Year='+ $( this ).attr('data-year')+ '&Class_Selected_All='+ $( this ).attr('data-all-centre')+ '&StartDate='+ $( this ).attr('report_date_start')+ '&EndDate='+ $( this ).attr('data-attendance-check-end'));
$( this ).attr( "href", '?module=module_progress_report&Subject='+ $('.subject-selected').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');
} );

上面的 JavaScript 有效,但所选主题的值始终为“2”,即第一个主题。

最佳答案

尝试一下,

// 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
}
});

更新了 anchor 标记循环,每次只获取 2 的原因是因为您正在使用 $('.subject-selected').val() 和jquery 将返回第一个复选框元素。因此,您需要使用 anchor 标记索引来获取其等效的复选框值。

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

关于javascript - ($ ('.class-name' ).is (":checked")) 从来不为真,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37408591/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com