我已经创建了一个函数来迭代UL/LI。这非常有效,我的问题是将值返回给另一个变量。这有可能吗?解决这个问题的最好方法是什么?谢谢!
function getMachine(color, qty) {
$("#getMachine li").each(function() {
var thisArray = $(this).text().split("~");
if(thisArray[0] == color&& qty>= parseInt(thisArray[1]) && qty<= parseInt(thisArray[2])) {
return thisArray[3];
}
});
}
var retval = getMachine(color, qty);发布于 2011-07-28 22:57:47
我不能完全确定该函数的一般用途,但您可以这样做:
function getMachine(color, qty) {
var retval;
$("#getMachine li").each(function() {
var thisArray = $(this).text().split("~");
if(thisArray[0] == color&& qty>= parseInt(thisArray[1]) && qty<= parseInt(thisArray[2])) {
retval = thisArray[3];
return false;
}
});
return retval;
}
var retval = getMachine(color, qty);发布于 2011-07-28 22:58:39
您拥有的return语句被卡在内部函数中,因此它不会从外部函数返回。你只需要更多的代码:
function getMachine(color, qty) {
var returnValue = null;
$("#getMachine li").each(function() {
var thisArray = $(this).text().split("~");
if(thisArray[0] == color&& qty>= parseInt(thisArray[1]) && qty<= parseInt(thisArray[2])) {
returnValue = thisArray[3];
return false; // this breaks out of the each
}
});
return returnValue;
}
var retval = getMachine(color, qty);https://stackoverflow.com/questions/6861039
复制相似问题