簡體   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