Jquery .each() - return value undefined

前端 未结 3 503
慢半拍i
慢半拍i 2020-12-12 04:51

Why getColorOptionSelect() return undefined value (I\'m sure it has a value by debugger ).

It is for sure an issue related to the scope, sorry for my js ignorance

3条回答
  •  被撕碎了的回忆
    2020-12-12 05:44

    Uncomment the last return statement to retun a value (something like selId)

    jQuery(document).ready(function () {
    
    colorSelectID = getColorOptionSelect();
    alert(colorSelectID);
    
    function getColorOptionSelect() {
    
        // get label
        var selId;
        jQuery(".product-options dl label").each(function () {
            el = jQuery(this);
            // lower case, remove *
            var labelText = el.text().toLowerCase().replace("*", "");
            if (labelText == 'color') {
                //return element
                selId = el.parent().next().find("select").attr('id');
                return false;  //<---  return false to stop further propagation of each
            }
        });
          return selId; //<---  Must return something 
    }
    
    });
    

提交回复
热议问题