简体   繁体   English

函数未返回值-未定义

[英]Function not returning the value - undefined

Did build a function to return the index of element from a group of elements. 确实建立了一个函数来从一组元素中返回元素的索引。 But when I try to pass the value to another variable (myindex) I do get undefined. 但是,当我尝试将值传递给另一个变量(myindex)时,我的确不确定。

Here the Javascript source code 这里的Javascript源代码

$(document).ready(function(){
             $("[id*='_CG']").addClass("form-control input-sm text-right");
    $("[id*='_Weight']").addClass("form-control input-sm text-right");
    $("[id*='TXFuelWeight']").addClass("form-control input-sm text-right");
    $("[id*='TRPFuelWeight']").addClass("form-control input-sm text-right");

      myindex = GetLSElementIndex("[id*='W_OP_']", "W_OP_CAB1Items");
alert (myindex);

            });
 // Get the index element in a group of element
 function GetLSElementIndex(ElementGroup, Element) {
    $(ElementGroup).each(function (index) {
        var someText = $(this).attr('id');
        if (someText == Element) {
            alert(someText);
            alert(index);
            //GetLSElementIndex = index;
            //alert(GetLSElementIndex);
            return ;
            //return index;

        }


    });

}

Example

You need to use like this: 您需要这样使用:

var indx;
if (someText == Element) {
    alert(someText);
    alert($('#'+Element).index());//Don't do just alert(index)
    indx = $('#'+Element).index();//I need to find index of ? #W_OP_CAB1Items
     return indx;
  }
function GetLSElementIndex(ElementGroup, Element) {
    var value = -1; //If no element is found, index is assumed to be -1
    $(ElementGroup).each(function (index) {
        var someText = $(this).attr('id');
        if (someText == Element) {
            alert(someText);
            value = index;
            alert(value);
            return false;//To break out of each() once the index is found.
        }
    });
    return value; //Return the value (from the called fn) once the processing is done

}

Rework your GetLSElementIndex method like this by defining one global variable for that method and assign that index of each loop to that and return it and then bring the return statement out of the each loop. 返修您GetLSElementIndex通过定义该方法一个全局变量的方法像这样与该索引分配each循环到并返回它,然后把return语句出的each循环。

DEMO: FIDDLE 演示: FIDDLE

// Get the index element in a group of element
    function GetLSElementIndex(ElementGroup, Element) {
        var setIndex;
        $(ElementGroup).each(function (index) {
            var someText = $(this).attr('id');
            setIndex = index;
            if (someText == Element) {
                alert(someText);
                alert(index);
            }
        });
        return setIndex;
    }

Note: Index of last item's of each loop only will be return by this method. 注意:此方法仅返回each循环的最后一项的索引。

You must break "Jquery each" and than return index back. 您必须打破“每个jQuery”,然后返回索引。 point of confusion is "Jquery each" break by "return false" statement. 混乱点是“ jQuery每次”被“ return false”语句破坏。 You must store index value in some local variable(var toReturn .. in function below). 您必须将索引值存储在某些局部变量中(以下函数中的var toReturn ..)。 here is the modified GetLSElementIndex function for you 这是为您修改的GetLSElementIndex函数

 // Get the index element in a group of element
 function GetLSElementIndex(ElementGroup, Element) {
   var toReturn; 
    $(ElementGroup).each(function (index) {
        var someText = $(this).attr('id');

       if (someText == Element) {
            //alert(someText);
           // alert(index);
            toReturn= index;
            //GetLSElementIndex = index;
            //alert(GetLSElementIndex);
            return  false ;
            //return index;

        }

    });
     return toReturn;

} 

for more details please follow other question of Stack Overflow 有关更多详细信息,请关注其他堆栈溢出问题

Update your function to store the index of desired element in a variable and then return the variable. 更新您的函数以将所需元素的索引存储在变量中,然后返回该变量。

function GetLSElementIndex(ElementGroup, Element) {
   var elementIndex;/* store the index of #element here */  
   $(ElementGroup).each(function (index) {
       var someText = $(this).attr('id');
       if (someText == Element) {
           elementIndex = index;
       }
   });
   return elementIndex; /* will return index of #element if found otherwise will return undefined */
}

Have removed all the alerts as I assume those are for debugging purposes. 我已删除所有警报,因为我认为这些警报是出于调试目的。

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

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