【问题标题】:Javascript incorporating jquery not working in Internet Explorer包含 jquery 的 Javascript 在 Internet Explorer 中不起作用
【发布时间】:2012-07-30 17:23:14
【问题描述】:

所以我一直在编写一个脚本,该脚本接收来自 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;
});

【问题讨论】:

  • 哪个版本的 IE 以及您得到的错误是什么?
  • 一些改进代码的建议。使用数组 q 而不是单个变量。另外,设置一个数组或映射(比如value),其中1 映射到0.132 映射到0.25 等等。那么你需要做的就是popScore = popScore - value[q[1]];

标签: javascript jquery internet-explorer


【解决方案1】:

浮点数不准确。您应该始终将它们四舍五入以进行显示。当我在我的 JS 控制台上用 chrome 输入 2.48+1.49 时,我得到了 3.9699999999999998。对于使用http://en.wikipedia.org/wiki/IEEE_754 的任何语言都是如此,这是使用最广泛的浮点格式。

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

【讨论】:

  • 我不认为这是问题所在。他正在使用toFixed(2)
  • @VivinPaliath OP 使用 toFixed(2) 因为浮点近似,但 OP 宁愿不必舍入它。我的回答解释说你必须把它四舍五入。
  • 我试图四舍五入,但我想这样做的唯一方法是使用toFixed(2)
  • @JuanMendes 啊,我以为他是在说toFixed(2) 没有在 IE 上工作。我的错!
  • @fudge22it toFixed 确实将其四舍五入到所要求的小数位数。除了你已经在做的事情之外,你无能为力。 developer.mozilla.org/en/JavaScript/Reference/Global_Objects/…
猜你喜欢
  • 1970-01-01
  • 2012-06-07
  • 2014-09-25
  • 2019-08-12
  • 1970-01-01
  • 2011-05-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多