簡體   English   中英

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

[英]Javascript incorporating jquery not working in Internet Explorer

因此,我一直在編寫一個腳本,該腳本從10個不同的字段獲取輸入,並且如果下拉菜單中的任何內容發生更改,那么popScore和popLevel都會相應地更新。 我遇到了一個小問題,它產生一個帶有10個小數的數字,因此我不得不將其四舍五入。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,但是就像我之前提到的,大多數語言都使用相同的格式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM