我有以下代码。函数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;
}
}
如果这是一个微不足道的问题,请原谅我。 在此先感谢。
答案 0 :(得分:1)
返回语句
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;
}
}