繁体   English   中英

树中的递归错误太多

[英]too much recursion error in tree

你好 stackoverflow 社区。 我需要有关“递归过多”错误的帮助。 当我执行这些功能时,很奇怪,但一切正常,只是错误。:

function check_checker (siblings, status) {
    if (siblings) {
        if (status == true) {
            $(siblings).children('li.imCheckbox').children('input').prop( "checked", true );
            if ($(siblings).children('ul')) {
                check_checker($(siblings).children('ul'), true);
            }                                           
        } else {
            $(siblings).children('li.imCheckbox').children('input').prop( "checked", false );
            if ($(siblings).children('ul')) {
                check_checker($(siblings).children('ul'), false);
            }                                           
        }
    }
}
$(document).ready(function(){
    $('body').on('click', 'input[name=impTaskCh]', function(){
        if ($(this).is(':checked')) {
            var siblingas = $(this).parent().siblings('ul');
            check_checker(siblingas, true);
        } else {
            var siblingas = $(this).parent().siblings('ul');
            check_checker(siblingas, false);
        }
    });
});

单击检查时,如果 ul 有 ul,它将检查所有复选框。 Maby check_checker 永无止境之类的? 大家怎么看?

是的,这永远不会结束。 $(siblings).children('ul')将返回一个对象,它是真实的,所以它总是真实的。 我建议改用 length 属性。

function check_checker (siblings, status) {
    if (siblings) {
        if (status == true) {
            $(siblings).children('li.imCheckbox').children('input').prop( "checked", true );
            if ($(siblings).children('ul').length > 0) {
                check_checker($(siblings).children('ul'), true);
            }                                           
        } else {
            $(siblings).children('li.imCheckbox').children('input').prop( "checked", false );
            if ($(siblings).children('ul').length > 0) {
                check_checker($(siblings).children('ul'), false);
            }                                           
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM