Javascript Jquery.each()-返回值未定义

Javascript Jquery.each()-返回值未定义,javascript,jquery,Javascript,Jquery,为什么getColorOptionSelect()返回未定义的值(我确信调试器为它提供了一个值) 这肯定是一个与范围相关的问题,很抱歉我的js无知 jQuery(document).ready(function () { colorSelectID = getColorOptionSelect(); alert(colorSelectID); function getColorOptionSelect() { // get label

为什么getColorOptionSelect()返回未定义的值(我确信调试器为它提供了一个值)

这肯定是一个与范围相关的问题,很抱歉我的js无知

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 selId;
            }
        });
        //    return null;
    }

});

getColorOptionSelect
没有(未注释的)
return
语句

唯一的返回语句是在传递给
each()
的匿名函数中。它将被
each()
(如果它是
false
,它将停止循环)下面的代码使用

这不是范围的问题,只是存在多个函数的问题

您可能想:

  • 在调用
    each()之前定义一个变量
  • 在每个循环中为其指定一个值
  • GetColorOptions Select

getColorOptionSelect
没有(未注释的)
return
语句

唯一的返回语句是在传递给
each()
的匿名函数中。它将被
each()
(如果它是
false
,它将停止循环)下面的代码使用

这不是范围的问题,只是存在多个函数的问题

您可能想:

  • 在调用
    each()之前定义一个变量
  • 在每个循环中为其指定一个值
  • GetColorOptions Select
您应该:

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; // to stop further execution of each
            }
        });
        return selId;
    }
在本例中,您正在执行从传递给每个的回调函数返回的操作,而不会从
getColorOptionSelect

从每个函数回调返回值的唯一方法是告诉jquery它是否应该转到下一项(
返回true;
)或不(
返回false;

,您应该执行以下操作:

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; // to stop further execution of each
            }
        });
        return selId;
    }
在本例中,您正在执行从传递给每个的回调函数返回的操作,而不会从
getColorOptionSelect


从每个函数回调返回值的唯一方法是告诉jquery它是否应该转到下一项(
return true;
)或不(
return false;

取消最后一条
return
语句的注释以重新运行值(类似于
selId

jQuery(文档).ready(函数(){
colorSelectID=getColorOptionSelect();
警报(colorSelectID);
函数getColorOptionSelect(){
//获取标签
变种selId;
jQuery(“.product options dl label”)。每个(函数(){
el=jQuery(this);
//下箱体,拆卸*
var labelText=el.text().toLowerCase().replace(“*”,”);
如果(labelText==“颜色”){
//返回元素
selId=el.parent().next().find(“select”).attr('id');

return false;//取消对最后一个
return
语句的注释以重新运行值(类似于
selId

jQuery(文档).ready(函数(){
colorSelectID=getColorOptionSelect();
警报(colorSelectID);
函数getColorOptionSelect(){
//获取标签
变种selId;
jQuery(“.product options dl label”)。每个(函数(){
el=jQuery(本);
//下箱体,拆卸*
var labelText=el.text().toLowerCase().replace(“*”,”);
如果(labelText==“颜色”){
//返回元素
selId=el.parent().next().find(“select”).attr('id');

return false;//每个都不支持那样的返回,因此出错。Toexit-Each需要返回false。每个都不支持那样的返回,因此出错。Toexit-Each需要返回false。