Javascript函数返回undefined而不是int

时间:2015-05-21 15:25:38

标签: javascript jquery recursion

我的js / jquery函数无法正常工作,而不是INT返回undefined。

function __getLastSelectedCategory(table_id) {
    if ( jQuery('.categories_table[data-table-id="1"]').find('td.active').length > 0 ){
        console.log('check1');
        if (table_id != '0') {
            console.log('check2');
            var checkTableId = parseInt(table_id) - 1;
            var table = jQuery('.cl_categories_display ').find('table[data-table-id="' + checkTableId + '"]');

            if (table.find('td.active').length > 0) {
                console.log('check3');
                console.log('table id: ' + table.find('td.active').data('category-id'));
                return table.find('td.active').data('category-id');
            } else {
                console.log('check4');
                __getLastSelectedCategory(checkTableId);
            }
        } else {
            console.log('check5');
            var lastTable = jQuery('.cl_categories_display ').find('table:last');
            var lastTableId = lastTable.data('table-id');

            if (lastTable.find('td.active').length > 0) {
                console.log('check6');
                return lastTable.find('td.active').data('category-id');
            } else {
                console.log('check7');
                __getLastSelectedCategory(lastTableId);
            }
        }
    } else {
        console.log('check8');
        return null;
    }
}

当我运行此功能时,我在控制台中看到:

  • check 1
  • check 5
  • check 7
  • check 1
  • check 2
  • check 3
  • table id:1
  • 最后一只猫:未定义

所以递归工作正常,但不是整数(控制台打印“table id:1”)ir返回undefined。可能有什么不对?

1 个答案:

答案 0 :(得分:2)

你从递归调用中忘记了return:它从内部函数返回值到外部,但没有将它从外部函数返回给调用者。试试这个:

    function __getLastSelectedCategory(table_id) {
        if ( jQuery('.categories_table[data-table-id="1"]').find('td.active').length > 0 ){
        console.log('check1');
        if (table_id != '0') {
            console.log('check2');
            var checkTableId = parseInt(table_id) - 1;
            var table = jQuery('.cl_categories_display ').find('table[data-table-id="' + checkTableId + '"]');

            if (table.find('td.active').length > 0) {
            console.log('check3');
            console.log('table id: ' + table.find('td.active').data('category-id'));
            return table.find('td.active').data('category-id');
            } else {
            console.log('check4');
            return __getLastSelectedCategory(checkTableId);
            }
        } else {
            console.log('check5');
            var lastTable = jQuery('.cl_categories_display ').find('table:last');
            var lastTableId = lastTable.data('table-id');

            if (lastTable.find('td.active').length > 0) {
            console.log('check6');
            return lastTable.find('td.active').data('category-id');
            } else {
            console.log('check7');
            return __getLastSelectedCategory(lastTableId);
            }
        }
        } else {
        console.log('check8');
        return null;
        }
    }
相关问题