简体   繁体   English

如何在jQuery / jS中使用if语句更改变量值

[英]how to change variable values using if statement in jQuery/jS

Hello I am having some trouble with my program. 您好,我的程序遇到了一些麻烦。 I've set some variables in the beginning of my program but Id like to change them with an if statement. 我在程序的开头设置了一些变量,但我想使用if语句更改它们。 Right now the first if statement gives me the results i want, but the second else if statement isnt working and the variables take the default values of '0' 现在,第一个if语句给我我想要的结果,但是第二个if语句不起作用,并且变量采用默认值'0'

here is the start of my jQuery code: 这是我的jQuery代码的开始:

    jQuery(".repair-grid-item1").click(function(){
         jQuery(".repair-grid-item1 i").toggle();
         jQuery(".repair-grid-item1").toggleClass("grid-active");
         jQuery('.repair-grid-item1').gg();
    });

   (function( $ ){
   $.fn.gg = function() {
      var price = 0;

      var one = 0;
      var two = 0;
      var three = 0;
      var four = 0;
      var five = 0;
      var six = 0;

      // *** this code block is working, returning correct values ***
      if (jQuery(".repair-grid-item").hasClass("iPhone5")) {
        var one = 85;
        var two = 70;
        var three = 75;
        var four = 65;
        var five = 75;
        var six = 80;
      }


      // *** this code block isnt working, returning default values of 0 ***
      else if (jQuery(".repair-grid-item").hasClass("iPhone5c")) {
        var one = 85;
        var two = 70;
        var three = 75;
        var four = 65;
        var five = 75;
        var six = 80;
      }

      //one only
      if (jQuery(".repair-grid-item1").hasClass("grid-active") &&
          !jQuery(".repair-grid-item2").hasClass("grid-active") &&
          !jQuery(".repair-grid-item3").hasClass("grid-active") &&
          !jQuery(".repair-grid-item4").hasClass("grid-active") &&
          !jQuery(".repair-grid-item5").hasClass("grid-active") &&
          !jQuery(".repair-grid-item6").hasClass("grid-active"))
      {
          price = one;
          document.getElementById("total-price").innerHTML = price;
          localStorage.setItem("sum", price);
      }
   };
})( jQuery );

here is a small snippet of my html code (this is the iPhone5 page) 这是我的html代码的一小段(这是iPhone5页面)

          <div class="repair-grid-item repair-grid-item1 iphone5">
            <h3>Screen Repair</h3>
            <i class="fa fa-check-circle-o fa-2x" aria-hidden="true"></i>
          </div>

iphone5c page: iphone5c页面:

          <div class="repair-grid-item repair-grid-item1 iphone5c">
            <h3>Screen Repair</h3>
            <i class="fa fa-check-circle-o fa-2x" aria-hidden="true"></i>
          </div>

thanks! 谢谢!

Variables in Javascript are function scoped. Javascript中的变量是函数范围的。 You don't have to declare them again using var inside your if , else-if blocks. 您无需在ifelse-if块内使用var再次声明它们。

Edit: As others have pointed out, the problem is with the case of the css class iphone5c . 编辑:正如其他人指出的那样,问题出在css类iphone5c的情况下。 You are checking for iPhone5c . 您正在检查iPhone5c

The problem is that you are checking for the class iPhone5c instead of the class iphone5c , note the capital P. JQuery is case sensitive, so you should writte them in with the same caps. 问题是,您正在检查类iPhone5c而不是类iphone5c ,请注意大写iphone5c区分大小写,因此您应该使用相同的大写字母。

So your your code is storing 0 on the variables because it doesn't pass the if else condition. 因此,您的代码将0存储在变量上,因为它没有通过if else条件。

Furthermore, as other answers say, due to the scope, variables don't need to be redeclared using var inside the if . 此外,正如其他答案所言,由于范围的原因,不需要在if使用var重新声明变量。 You should remove that. 您应该删除它。

I'm sorry guys i made a silly error, @nnnnnn pointed me in the right direction. 抱歉,我犯了一个愚蠢的错误,@ nnnnnn向我指出了正确的方向。 i was not consistent in my cases, 'iPhone5c' uppercase P was in my jQuery code while 'iphone5c' lowercase p was in my html. 我在我的情况下不一致,“ iPhone5c”大写字母P在我的jQuery代码中,而“ iphone5c”小写字母p在我的html中。 I corrected the cases and it works fine now. 我更正了案件,现在工作正常。 Also you guys were also correct, I didnt need to have 'var' infront of each veriable in the if and else if cases. 你们也是正确的,在if和else if情况下,我不需要在每个Veriable前面都添加'var'字样。 Thanks for helping! 感谢您的帮助! and thanks @nnnnnn for finding the solution :) 并感谢@nnnnnn找到解决方案:)

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

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