简体   繁体   中英

Javascript function return value is undefined

I've the following code. The return value of the function get_last_catergory_value is always undeifned . I've searched stackoverflow but couldn't debug the issue.

When I show the value being returned just before the return statement it shows the correct value. But when it is returned from function it is undefined . Can you please help me to resolve this?

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

Forgive me if its a trivial issue. Thanks in Advance.

return statement is missing in the if block of get_last_catergory_value(last_category)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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