Javascript 我想访问if()迭代器外部的变量

Javascript 我想访问if()迭代器外部的变量,javascript,jquery,Javascript,Jquery,我希望从if循环中获取if()中的当前值 $('.name').each(function () { //name = this; temp = 13; alert("begins with" + temp); //console.log(temp) if (($(this).hasClass("1") == true))

我希望从if循环中获取if()中的当前值

        $('.name').each(function ()
        {
            //name = this;
            temp = 13;
            alert("begins with" + temp);
            //console.log(temp)

            if (($(this).hasClass("1") == true))
            {
                //console.log("you in1")
                //console.log(temp);
                if (temp > 10 && temp <= 20)
                {
                    temp = temp + 10;
                    alert("temp modified to: " + temp);
                    //console.log(temp);
                    //Agriculture.push(10);
                }
                else
                {
                    temp = temp + 5;
                    //console.log(temp);
                    //Agriculture.push(5);
                }
                //return temp;
                console.log(temp);
                alert("in first if: " + temp);
            }

            if (($(this).hasClass("2") == true))
            {
                console.log("you in2")
                if (temp > 10 && temp <= 20)
                {
                    console.log(temp);
                    alert("current value should match the previous alert: " + temp);
                    temp = temp + 10;
                    //Industry.push(10);
                }
                else
                {
                    temp = temp + 5;
                    //Industry.push(5);
                }
                alert("in second if: " + temp);
                //return temp;
            }
        });
</script>
例如:

        $('.name').each(function ()
        {
            //name = this;
            temp = 13;
            alert("begins with" + temp);
            //console.log(temp)

            if (($(this).hasClass("1") == true))
            {
                //console.log("you in1")
                //console.log(temp);
                if (temp > 10 && temp <= 20)
                {
                    temp = temp + 10;
                    alert("temp modified to: " + temp);
                    //console.log(temp);
                    //Agriculture.push(10);
                }
                else
                {
                    temp = temp + 5;
                    //console.log(temp);
                    //Agriculture.push(5);
                }
                //return temp;
                console.log(temp);
                alert("in first if: " + temp);
            }

            if (($(this).hasClass("2") == true))
            {
                console.log("you in2")
                if (temp > 10 && temp <= 20)
                {
                    console.log(temp);
                    alert("current value should match the previous alert: " + temp);
                    temp = temp + 10;
                    //Industry.push(10);
                }
                else
                {
                    temp = temp + 5;
                    //Industry.push(5);
                }
                alert("in second if: " + temp);
                //return temp;
            }
        });
</script>
$('.name')。每个(函数(){
//name=这个;
temp=parseInt($(“#此处为幻灯片”).next().val();
//控制台日志(临时)
if($(this.hasClass(“1”)==true)){
//log(“你在1中”)
//控制台日志(temp);

if(temp>10&&temp 10&&temp将全局变量的赋值移到外部。在代码中,当值大于20时,它不会输入if语句。为了使enter进入第二个if,我更新了条件,以查看全局变量与上次更新的值保持相同

var temp = parseInt($('#slide_here').next().val());

$('.name').each(function () {
            //name = this;



            //console.log(temp)

            if (($(this).hasClass("1") == true)) {

                //console.log("you in1")
                //console.log(temp);
                if (temp > 10 && temp <= 20) {
                    temp = temp + 10;
                    //console.log(temp);
                    //Agriculture.push(10);
                } else {
                    temp = temp + 5;
                    //console.log(temp);
                    //Agriculture.push(5);
                }
                //return temp;
                console.log(temp);

            }

            if (($(this).hasClass("2") == true)) {

                console.log("you in2")
                if (temp > 10 && temp <= 20) {
                    console.log(temp);
                    temp = temp + 10;
                    //Industry.push(10);
                } else {
                    temp = temp + 5;
                    //Industry.push(5);
                }
                //return temp;
            }
        $('.name').each(function ()
        {
            //name = this;
            temp = 13;
            alert("begins with" + temp);
            //console.log(temp)

            if (($(this).hasClass("1") == true))
            {
                //console.log("you in1")
                //console.log(temp);
                if (temp > 10 && temp <= 20)
                {
                    temp = temp + 10;
                    alert("temp modified to: " + temp);
                    //console.log(temp);
                    //Agriculture.push(10);
                }
                else
                {
                    temp = temp + 5;
                    //console.log(temp);
                    //Agriculture.push(5);
                }
                //return temp;
                console.log(temp);
                alert("in first if: " + temp);
            }

            if (($(this).hasClass("2") == true))
            {
                console.log("you in2")
                if (temp > 10 && temp <= 20)
                {
                    console.log(temp);
                    alert("current value should match the previous alert: " + temp);
                    temp = temp + 10;
                    //Industry.push(10);
                }
                else
                {
                    temp = temp + 5;
                    //Industry.push(5);
                }
                alert("in second if: " + temp);
                //return temp;
            }
        });
</script>
var temp=parseInt($('slide#u here').next().val());
$('.name')。每个(函数(){
//name=这个;
//控制台日志(临时)
if($(this.hasClass(“1”)==true)){
//log(“你在1中”)
//控制台日志(temp);

if(temp>10&&temp 10&&temp这似乎是正确的。我看到的唯一问题是您检查temp是否小于21,但现在是23,因此它不会进入最后一个if。添加一些警报以调试它。以下是对我有效的方法:

        $('.name').each(function ()
        {
            //name = this;
            temp = 13;
            alert("begins with" + temp);
            //console.log(temp)

            if (($(this).hasClass("1") == true))
            {
                //console.log("you in1")
                //console.log(temp);
                if (temp > 10 && temp <= 20)
                {
                    temp = temp + 10;
                    alert("temp modified to: " + temp);
                    //console.log(temp);
                    //Agriculture.push(10);
                }
                else
                {
                    temp = temp + 5;
                    //console.log(temp);
                    //Agriculture.push(5);
                }
                //return temp;
                console.log(temp);
                alert("in first if: " + temp);
            }

            if (($(this).hasClass("2") == true))
            {
                console.log("you in2")
                if (temp > 10 && temp <= 20)
                {
                    console.log(temp);
                    alert("current value should match the previous alert: " + temp);
                    temp = temp + 10;
                    //Industry.push(10);
                }
                else
                {
                    temp = temp + 5;
                    //Industry.push(5);
                }
                alert("in second if: " + temp);
                //return temp;
            }
        });
</script>
$('.name')。每个(函数()
{
//name=这个;
温度=13;
警报(“以“+temp”开头);
//控制台日志(临时)
if($(this.hasClass(“1”)==true))
{
//log(“你在1中”)
//控制台日志(temp);

if(temp>10&&temp 10&&temp
if
语句不是循环-它们执行一次(取决于条件是否为真),并且没有迭代

        $('.name').each(function ()
        {
            //name = this;
            temp = 13;
            alert("begins with" + temp);
            //console.log(temp)

            if (($(this).hasClass("1") == true))
            {
                //console.log("you in1")
                //console.log(temp);
                if (temp > 10 && temp <= 20)
                {
                    temp = temp + 10;
                    alert("temp modified to: " + temp);
                    //console.log(temp);
                    //Agriculture.push(10);
                }
                else
                {
                    temp = temp + 5;
                    //console.log(temp);
                    //Agriculture.push(5);
                }
                //return temp;
                console.log(temp);
                alert("in first if: " + temp);
            }

            if (($(this).hasClass("2") == true))
            {
                console.log("you in2")
                if (temp > 10 && temp <= 20)
                {
                    console.log(temp);
                    alert("current value should match the previous alert: " + temp);
                    temp = temp + 10;
                    //Industry.push(10);
                }
                else
                {
                    temp = temp + 5;
                    //Industry.push(5);
                }
                alert("in second if: " + temp);
                //return temp;
            }
        });
</script>
代码中的循环实际上是对这一行中每个jQuery
的调用:

        $('.name').each(function ()
        {
            //name = this;
            temp = 13;
            alert("begins with" + temp);
            //console.log(temp)

            if (($(this).hasClass("1") == true))
            {
                //console.log("you in1")
                //console.log(temp);
                if (temp > 10 && temp <= 20)
                {
                    temp = temp + 10;
                    alert("temp modified to: " + temp);
                    //console.log(temp);
                    //Agriculture.push(10);
                }
                else
                {
                    temp = temp + 5;
                    //console.log(temp);
                    //Agriculture.push(5);
                }
                //return temp;
                console.log(temp);
                alert("in first if: " + temp);
            }

            if (($(this).hasClass("2") == true))
            {
                console.log("you in2")
                if (temp > 10 && temp <= 20)
                {
                    console.log(temp);
                    alert("current value should match the previous alert: " + temp);
                    temp = temp + 10;
                    //Industry.push(10);
                }
                else
                {
                    temp = temp + 5;
                    //Industry.push(5);
                }
                alert("in second if: " + temp);
                //return temp;
            }
        });
</script>
$('.name').each(function () {
如果您希望在循环中更新
temp
的值,而不必为类为
name
的每个元素重置该值,则可能需要将
temp
变量的声明移到循环之外:

        $('.name').each(function ()
        {
            //name = this;
            temp = 13;
            alert("begins with" + temp);
            //console.log(temp)

            if (($(this).hasClass("1") == true))
            {
                //console.log("you in1")
                //console.log(temp);
                if (temp > 10 && temp <= 20)
                {
                    temp = temp + 10;
                    alert("temp modified to: " + temp);
                    //console.log(temp);
                    //Agriculture.push(10);
                }
                else
                {
                    temp = temp + 5;
                    //console.log(temp);
                    //Agriculture.push(5);
                }
                //return temp;
                console.log(temp);
                alert("in first if: " + temp);
            }

            if (($(this).hasClass("2") == true))
            {
                console.log("you in2")
                if (temp > 10 && temp <= 20)
                {
                    console.log(temp);
                    alert("current value should match the previous alert: " + temp);
                    temp = temp + 10;
                    //Industry.push(10);
                }
                else
                {
                    temp = temp + 5;
                    //Industry.push(5);
                }
                alert("in second if: " + temp);
                //return temp;
            }
        });
</script>
var temp = parseInt($('#slide_here').next().val());

$('.name').each(function () {
    if (($(this).hasClass("1") == true)) {
        // ... do logic and update temp
    }
    if (($(this).hasClass("2") == true)) {
        // ... do logic and update temp
    }
}

// use final temp value

有几个if块和几个变量。请澄清。另外,if不是迭代器。
.name
classed元素是否有您正在寻找的两个类
1
2
中的任何一个?按数字命名类不是一个好主意。另外,请在我发布答案之前发布您的HTMLB,有一个返回在主if($(this.hasClass(“1”)==true)){
if
不是一个循环,因为第二个
if
中只得到值13,所以它不会进入第一个
if
块。在发布我的答案之前,有一个返回语句。它是uncommented@scrowler在每个
内的操作代码中,在
temp
之前没有
var
回调,所以它必须是全局的。问题可能是在每个
回调开始时,temp被重置为滑块值。你错了。请参阅这里的演示:检查控制台,如果前一个if的23个值应添加到second if temp,则会在第二次迭代中看到它是23,这意味着它应该是23+5=28 be由于条件为false。但我已将警报放在它们前面。请参阅此处的演示,了解第一个if语句的工作情况良好,,,,,,,,但下一个if语句的值未得到更新。是否确定元素同时具有“1”和“2”类?您可以通过在每个if块中添加console.log或alert调用来检查这两个条件的计算结果是否为true。在second if中,前一个if的23值应添加到second if temp,这意味着它应该为23+5=28,因为条件为false