【发布时间】:2014-07-01 10:15:27
【问题描述】:
我必须在相应区域下显示zone names 和regions。我在每个区域的右侧以及区域的一侧都有用于选择区域和区域的复选框。当我检查特定的zone 时,我只需要检查该特定区域下的所有regions。我正在显示数据库中的区域数据和相应的区域。
我的php代码是这样的:
//显示区域名称
<table cellpadding="15" cellspacing="10">
<?php
$last_route = '';
$getbus = mssql_query("SELECT brd.BusrouteID,BusRoute,brd.AreaID,br.BusRoute,a.AreaName_1 FROM dbo.Acc_BusRouteDetail brd JOIN dbo.PRTL_BusRoute br ON br.busrouteid = brd.busrouteid JOIN dbo.GEN_Area a ON a.areaid = brd.areaid ORDER BY br.busroute, a.areaname_1");
while($data_getbus = mssql_fetch_array($getbus)){
if($last_route != $data_getbus['BusRoute']) {
$last_route = $data_getbus['BusRoute'];//last_route contain zone names
echo '<tr><td><label for="'.$data_getbus[BusrouteID].'"><h4>'.$last_route.'</h4> </label></td><td><input type="checkbox" class="zone_check" id="'.$data_getbus[BusrouteID].'" name="zone[]" value="'.$data_getbus[BusrouteID].'"></td></tr><tr>';
}
echo '<td><label for="'.$data_getbus[BusrouteID]."_".$data_getbus[AreaID].'">'.$data_getbus['AreaName_1'].'</label></td>
<td><input type="checkbox" id="'.$data_getbus[BusrouteID]."_".$data_getbus[AreaID].'" name="arealist[]" value="'.$data_getbus[BusrouteID]."_".$data_getbus[AreaID].'" >'."</td></tr>";
}
?>
</table>
我的 Jquery 是这样的:
$(".zone_check").change(function(){
var checkboxes = $(this).children().find(':checkbox');
if($(this).prop('checked')) {
checkboxes.prop('checked', true);
} else {
checkboxes.prop('checked', false);
}
});
【问题讨论】:
-
var checkboxes = $(this).children().find(':checkbox')不正确,因为this是不能有子元素的复选框 -
猜测您没有提供正确的 HTML。尝试使用
$(this).closest('table').find(':checkbox[name="arealist[]"]')而不是$(this).children().find(':checkbox') -
可以分享一下生成的html吗
-
@Satpal 当我使用你的代码时,它会在我检查单个区域时选择所有区域。我只需要检查该特定区域下的区域
-
@ArunPJohny 我已经编辑了 html