【发布时间】:2013-08-23 16:43:18
【问题描述】:
我的目标是在嵌套列表中“支持”父复选框。现在我在 PHP 中拉入一个目录列表,如下所示:
function listDirectories($dir, $i, $howDeep) {
$lastFolder = end(explode("/", $dir));
$listString .='<li class="closed"><span class="folder"><input type="checkbox" class="userPermissionCheckBox" parent="'.$i.'" howDeep="'.$howDeep.'" value="'.$dir.'" />'.str_replace('_', ' ', $lastFolder).'</span>';
foreach (glob($dir."/*", GLOB_ONLYDIR) as $d) {
$howDeep++;
$listString .='<ul>';
$listString .=' '.listDirectories($d, $i, $howDeep).' ';
$listString .='</ul>';
$i++;
$howDeep = 1;
}
$listString .='</li>';
return $listString;
}
这太棒了!,然后我将此 jquery/js 函数绑定到复选框,以在选择父级时“支持”所有子框,如下所示:
var isAlreadyChecked = $(this).prop('checked');
$(this).parents(':eq(1)').find('.userPermissionCheckBox').each(function () {
$(this).prop('checked', isAlreadyChecked);
});
这也很棒。
我卡住的地方是如果有人在不检查父框的情况下检查子框,它会自动检查父框......所以我尝试了这样的事情:
var isAlreadyChecked = $(this).prop('checked');
var parentNumber = Number($(this).attr('parent'));
var howDeepIsNest = Number($(this).attr('howDeep'));
$(this).parents(':eq(1)').find('.userPermissionCheckBox').each(function () {
$(this).prop('checked', isAlreadyChecked);
});
if(howDeepIsNest > 1){
var immediateParent = howDeepIsNest - 1;
$('.userPermissionCheckBox[howDeep='+immediateParent+']').prop('checked', true);
}
这种工作方式,显然子框的自动道具效果很好,但我无法自动检查父框的工作。我愿意接受任何建议。感谢您抽出宝贵时间阅读本文。
【问题讨论】:
标签: php javascript jquery