包含jquery的Javascript在Internet Explorer中不起作用

Javascript incorporating jquery not working in Internet Explorer

本文关键字:Explorer 不起作用 Internet jquery Javascript 包含      更新时间:2024-05-23

因此,我一直在编写一个脚本,该脚本接受来自10个不同字段的输入,如果下拉列表中的任何内容发生了更改,则popScore和popLevel将相应更新。我遇到了一个小问题,它产生了一个大约有10个小数的数字,所以我不得不把它四舍五入到2。javascript适用于除Internet Explorer之外的所有浏览器是什么原因导致它在Internet Explorer上失败

下面我只为其中一个下拉列表包含了javascript以节省空间,但它们之间的区别是popScore的值和下拉列表中的项目数。popLevel最多只能达到5。

对于理解为什么它不起作用的任何帮助,我们都将不胜感激。谢谢

 <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;
});

浮点数不精确。为了便于展示,您应该始终将它们四舍五入。当我在chrome中的JS控制台上键入2.48+1.49时,我得到3.9699999999999998。这适用于任何使用http://en.wikipedia.org/wiki/IEEE_754,这是使用最广泛的浮点格式。

以下是对问题的一个很好的解释:http://docs.python.org/tutorial/floatingpoint.html它适用于Python,但正如我之前提到的,大多数语言都使用相同的格式。