简体   繁体   English

合并jquery的Javascript在Internet Explorer中不起作用

[英]Javascript incorporating jquery not working in Internet Explorer

So I have been working on writing a script that takes in input from 10 different fields and if anything on the dropdown is changed a popScore and popLevel gets updated accordingly. 因此,我一直在编写一个脚本,该脚本从10个不同的字段获取输入,并且如果下拉菜单中的任何内容发生更改,那么popScore和popLevel都会相应地更新。 I ran into a little issue with it producing a number with like 10 decimals so I had to round it to 2. The javascript works on all browsers except Internet Explorer. 我遇到了一个小问题,它产生一个带有10个小数的数字,因此我不得不将其四舍五入。javascript在除Internet Explorer之外的所有浏览器上均可使用。 What causes it to fail on Internet Explorer? 是什么导致它在Internet Explorer上失败?

Below I included the javascript for only one of the dropdowns to save space but the difference between them is the values for the popScore and the number of items in the dropdown. 下面,我仅包含了其中一个下拉菜单的javascript,以节省空间,但它们之间的区别在于popScore的值和下拉列表中的项目数。 The popLevel can only go up to 5. popLevel只能上升到5。

Any help on understanding why it doesn't work is greatly appreciated. 非常感谢您对理解为什么不起作用的任何帮助。 Thanks. 谢谢。

 <script type="text/javascript">
 $(document).ready(function(){
  //global variables
  var popLevel = 0;
  var popScore = 0.00;
  var q1=0;
  var q2=0;
  var q3=0;
  var q4=0;
  var q5=0;
  var q6=0;
  var q7=0;
  var q8=0;
  var q9=0;
  var q10=0;
$('.selectionbox1').change(function() {
    if(q1 != 0){
        if(q1 == 1){
        popScore = popScore - 0.13;
        }
        else if(q1 == 2){
        popScore = popScore - 0.25;
        }
        else if(q1 == 3){
        popScore = popScore - 0.38;
        }
        else if(q1 == 4){
        popScore = popScore - 0.50;
        }
        else if(q1 == 5){
        popScore = popScore - 0.63;
        }
    }
    var b = document.getElementById("q1").value;
    if(b == 1){
        popScore = popScore + 0.13;
    }
    else if(b == 2){
        popScore = popScore + 0.25;
        }
    else if(b == 3){
        popScore = popScore + 0.38;
    }
    else if(b == 4){
        popScore = popScore + 0.50;
        }
    else if(b == 5){
        popScore = popScore + 0.63;
    }

    if(popScore > 4.00){
    popLevel=5;
    }
    else if(popScore > 3.00){
    popLevel=4;
    }
    else if(popScore > 2.00){
    popLevel=3;
    }
    else if(popScore > 1.00){
    popLevel=2;
    }
    else if(popScore > 0.00){
    popLevel=1;
    }

    //record the current value of the field changed
    var e = document.getElementById("q1");
    q1=e.options[e.selectedIndex].value;

    //round to 2 decimal places for score
    popScore = parseFloat(popScore.toFixed(2));

    //update the fields for pop score and pop level
    var txt = document.getElementById("popLevel");
    txt.innerHTML=popLevel;
    var txt2 = document.getElementById("popScore");
    txt2.innerHTML=popScore;
});

Floating numbers are not exact. 浮点数不准确。 You should always round them for display purposes. 您应该始终将它们四舍五入以用于显示。 When I type 2.48+1.49 on my JS console in chrome, I get 3.9699999999999998 . 当我在Chrome的JS控制台上输入2.48+1.49时,得到3.9699999999999998 This is true for any language that uses http://en.wikipedia.org/wiki/IEEE_754 , which is the most widely used floating point format. 对于使用http://en.wikipedia.org/wiki/IEEE_754的任何语言都是如此,这是使用最广泛的浮点格式。

Here's a good explanation of the problem: http://docs.python.org/tutorial/floatingpoint.html It's for Python, but like I mentioned before, most languages use the same format. 这里是问题的一个很好的解释: http : //docs.python.org/tutorial/floatingpoint.html它适用于Python,但是就像我之前提到的,大多数语言都使用相同的格式。

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

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