繁体   English   中英

JavaScript函数返回值未定义

[英]Javascript function return value is undefined

我有以下代码。 函数get_last_catergory_value的返回值始终undeifned 我搜索了stackoverflow,但无法调试问题。

当我在return语句之前显示要返回的值时,它显示正确的值。 但是当它从函数返回时,它是undefined 您能帮我解决这个问题吗?

 function fetch_product(brand) { var brand = brand; //get last category id var last_category = 'subcategory10'; var last_category_value = get_last_catergory_value(last_category); alert(last_category_value); //undefined } function get_last_catergory_value(last_category) { if($('.' + last_category).find(':selected').val() == 'none') { last_category_number = last_category.substring(11); last_category_number = parseInt(last_category_number); last_category_number--; last_category = last_category.substring(0, 11); last_category = last_category + last_category_number; get_last_catergory_value(last_category); //recall the function with values 'subcategory9', 'subcategory8' and so on... } else { var value = $('.' + last_category).find(':selected').val(); alert(value); //Gives the correct value here return value; } } 

如果这是一个小问题,请原谅我。 提前致谢。

get_last_catergory_value(last_category)的if块中缺少return语句

 function get_last_catergory_value(last_category) {

if($('.' + last_category).find(':selected').val() == 'none') {

    last_category_number = last_category.substring(11);
    last_category_number = parseInt(last_category_number);
    last_category_number--;

    last_category = last_category.substring(0, 11);
    last_category = last_category + last_category_number;
    return get_last_catergory_value(last_category);
} else {
    var value = $('.' + last_category).find(':selected').val();

    alert(value); //Gives the correct value here
    return value;
}
}

暂无
暂无

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

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