I'm trying to create jquery variables using min and max values.
我正在尝试使用min和max值创建jquery变量。
To explain what i am trying to do I've created this JSFIDDLE
为了解释我想要做什么,我已经创建了这个JSFIDDLE
and this is my entire code:
这是我的整个代码:
function minMaxId(selector) {
var min=null, max=null;
$(".allPro").each(function() {
var price = parseInt($(this).attr("data-price"), 10);
if (isNaN(price)) { return; }
if ((min===null) || (price < min)) { min = price;}
if ((max===null) || (price > max)) { max = price;}
});
return [min, max];
var maximum = max;
var minimum = min;
}
alert(minimum);
when i run my code, i get no alert();
at all and when i look in the console, i get minimum is undifined
当我运行我的代码时,我得不到警报();在我看到控制台的时候,我得到的最小化就是不分割
could someone please let me know what i am doing wrong?
有人可以让我知道我做错了什么吗?
1 个解决方案
#1
2
Call function minMaxId
and assign result to variable ans then use it
调用函数minMaxId并将结果赋给变量ans然后使用它
function minMaxId(selector) {
var min=null, max=null;
$(".allPro").each(function() {
var price = parseInt($(this).attr("data-price"), 10);
if (isNaN(price)) { return; }
if ((min===null) || (price < min)) { min = price;}
if ((max===null) || (price > max)) { max = price;}
});
return [min, max];
}
var result = minMaxId();
var maximum = result[1];
var minimum = result[0];
console.log(minimum);
console.log(maximum);
Because in your variant function return value and you are trying assign variables after return, also you are using var
- in this case this variables will be only in function scope
因为在你的variant函数返回值并且你在返回后尝试赋值变量,你也使用var - 在这种情况下这个变量只在函数范围内
#1
2
Call function minMaxId
and assign result to variable ans then use it
调用函数minMaxId并将结果赋给变量ans然后使用它
function minMaxId(selector) {
var min=null, max=null;
$(".allPro").each(function() {
var price = parseInt($(this).attr("data-price"), 10);
if (isNaN(price)) { return; }
if ((min===null) || (price < min)) { min = price;}
if ((max===null) || (price > max)) { max = price;}
});
return [min, max];
}
var result = minMaxId();
var maximum = result[1];
var minimum = result[0];
console.log(minimum);
console.log(maximum);
Because in your variant function return value and you are trying assign variables after return, also you are using var
- in this case this variables will be only in function scope
因为在你的variant函数返回值并且你在返回后尝试赋值变量,你也使用var - 在这种情况下这个变量只在函数范围内