在每个语句之外无法识别var值

时间:2019-02-02 21:36:34

标签: jquery

当我在.each语句内设置var sfp的值时,该值不会在.each语句外使用。

这是一段代码:

$(document).ready(function() {

//using this as testing array
var sspricebreaks = [
"Single Sided Banner",
["0","20","50"],
["3","2","1"]
]

var updateprice = function(){
var h = 24
var w = 48
var q = 5
var tsf = ((h*w)/144)*q;
var sfe = (h*w)/144;
var sfp;

//I am using .reverse because the array is least to greatest but I only need the greatest matched value
$.each(sspricebreaks[1].reverse(), function (index, value) {
    var usevalue;
    if (tsf <= value) {
        var usevalue = ( jQuery.inArray( value, sspricebreaks[1].reverse() ) );

    //this is where I am getting the value for sfp but the value is not updating outside of the .each statement
    var sfp = sspricebreaks[2][usevalue]; 
    return false;
    }
})  

var t = Number.parseFloat(sfe*sfp).toFixed(2);
if (t <= 0) { //Fallback
    t = "0.00";
}
$("#price").text(t);
}

}

我需要 var sfp 可以在函数末尾的 var t = 语句中使用,但它只会返回“未定义”

0 个答案:

没有答案