Javascript函数返回undefined而不是int

Javascript function returns undefined instead of int

本文关键字:int undefined 函数 返回 Javascript      更新时间:2023-09-26

我的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;
    }
}

当我运行这个函数时,我在控制台上看到:

  • 检查1
  • 检查5
  • 检查7
  • 检查1
  • 检查2
  • 检查3
  • 表id: 1
  • last cat: undefined

所以递归工作良好,但不是整数(控制台打印"表id: 1"),它返回未定义。会出什么问题呢?

您忘记了递归调用的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;
        }
    }