2012-03-24 3 views
0

파이어 폭스와 크롬에서 잘 동작하는 다음 자바 스크립트 코드가 있습니다. 하지만 IE에서이 페이지를로드하면 모든 필드가 NaN으로 채워집니다. 어떤 도움을 주시면 감사하겠습니다.Javascript IE에서는 NaN이지만 크롬, Firefox에서는 괜찮습니다.

var totalarray=new Array(); 
    var totalarray2= new Array(); 
    var runningtotal = 0; 
    var runningtotal2 = 0; 
    var discount = .2; 
    var discounttotal = 0; 
    var discount1 = 0; 

    runningtotal = runningtotal * 1; 
    runningtotal2 = runningtotal2 * 1; 
     function displayResult(price,init) 
     { 
     var newstring = "quantity"+init; 
     var totstring = "total"+init; 
     var quantity = document.getElementById(newstring).value; 
     var quantity = parseFloat(quantity); 
     var test = price * quantity; 
     var test = test.toFixed(2); 
     document.getElementById(newstring).value = quantity; 
     document.getElementById(totstring).value = "$" + test; 
     totalarray[init] = test; 
     getTotal(); 
     } 
     function getTotal(){ 
     runningtotal = 0; 
     var i=0; 
     for (i=0;i<totalarray.length;i++){ 
      totalarray[i] = totalarray[i] *1; 
      runningtotal = runningtotal + totalarray[i]; 
      discounttotal = totalarray[i] * discount; 
      discounttotal = totalarray[i] - discounttotal; 
      document.getElementById('totalcost').value="$" + runningtotal.toFixed(2); 
      } 
      var orderpart1 = document.getElementById('totalcost').value; 
      var orderpart1 = orderpart1.substr(1); 
       var orderpart1 = orderpart1 * 1; 
     var orderpart2 = document.getElementById('totalcost2').value; 
     var orderpart2 = orderpart2.substr(1); 
     var orderpart2 = orderpart2 * 1; 
     var ordertot = orderpart1 + orderpart2; 
     document.getElementById('ordertotal').value ="$"+ ordertot.toFixed(2) 
     } 

이것은 위 스크립트를 호출하는 PHP 코드입니다. 기본적으로 합계를 결정하는 드롭 다운 메뉴입니다.

<table border='0' cellpadding='0' cellspacing='0'> 
    <tr> 
     <td> 
      <table width="100%" border="1"> 
      <tr> 
       <th>Item&nbsp&nbsp&nbsp&nbsp&nbsp</th> 
       <th>Price&nbsp&nbsp&nbsp&nbsp&nbsp </th> 
       <th>Quantity&nbsp&nbsp&nbsp&nbsp&nbsp</th> 
       <th>Total&nbsp&nbsp&nbsp&nbsp&nbsp</th> 
      </tr> 
    <?php 
    mysql_connect("localhost", "***", "****") or die(mysql_error()); 
    mysql_select_db("fsenior") or die(mysql_error()); 
    $result = mysql_query("SELECT name, price,foodid FROM Food where foodtype='br'") or die(mysql_error()); 
    $init = 0; 
    while(list($name, $price, $brId) = mysql_fetch_row($result)) { 
    echo 
     "<tr> 
      <td>$name</td> 
      <td>\$$price</td> 
      <td><select name='quantity$init' id='quantity$init' onchange='displayResult($price,$init)'><option>0</option><option>1</option><option>2</option><option>3</option><option>4</option><option>5</option><option>6</option><option>7</option><option>8</option><option>9</option></td> 
      <td><input name='total$init' type='text' id='total$init' readonly='readonly' value='\$0.00'></td>  
     </tr>" ; 

    echo "<script type='text/javascript'>displayResult($price,$init);</script>"; 
    $foodname = "'SESS_FOODNAME_" . $init . "'"; 
    $foodid = "'SESS_FOODID_" . $init."'"; 
    $_SESSION[$foodname] = $name; 
    $_SESSION[$foodid] = $brId; 
    $init = $init+1; 
    } 
    $_SESSION['SESS_INIT'] = $init; 

    ?> 

     <tr> 
      <td></td> 
      <td></td> 
      <td>Total Cost</td> 
      <td><input name='totalcost' type='text' id='totalcost' readonly='readonly' value='$0.00'></td> 
     </tr> 
     <tr><td></td><td></td><td>Discount</td><td><input name='discountvalue1' id ='discountvalue1' type='text' readonly='readonly' value='20%'></td> </tr> 
     <tr><td></td><td></td><td>Total After Discount</td><td><input name='discounttotal1' id ='discounttotal1' type='text' readonly='readonly' value='$0.00'></td></tr> 
     </table> 
<tr> 
<td><br></td> 
</tr> 
</table> 
+0

오류가 스크립트를 호출하는 코드를 게시 할 수 –

+0

를 발생합니까? – j08691

+0

행에 document.getElementById ('totalcost'). value = "$"+ runningtotal.toFixed (2); document.getElementById ('totalcost2'). value = "$"+ runningtotal2.toFixed (2); – Zinv

답변

0

확실하지 않지만 촬영 가치가있을 수 있습니다. 대신 :

document.getElementById('xxx').value = "$" + runningtotal2.toFixed(2); 

시도 :

document.getElementById('xxx').value = "$" + runningtotal2.toFixed(2).toString(); 

또는 : 충돌 유형 (에 this post를 볼 수있는 경우

document.getElementById('xxx').value = "$" + (runningtotal2.toFixed(2) + ''); 

이론적으로 + 연산자는 문자열 연결 동작에 우선 순위를 부여 할 몇 가지 예를 들겠습니다.)하지만 MSIE 만 있으면 테스트 할 수 있습니다.

행운;) 무엇을 줄

+0

입력 주셔서 감사. 문제가 뭔지 알아 냈어. 드롭 박스에서 변수 수량을 설정하기 위해 getElementById를 사용했기 때문에 null로 설정되었습니다. 수정하려면 다음을 수행해야했습니다. 원본 var 수량 = document.getElementById (newstring) ;; 그리고 이것을 다음으로 변경하십시오. var quanttest = document.getElementById (newstring) .selectedIndex; \t var 수량 = document.getElementById (newstring) .options [quanttest] .text; – Zinv

관련 문제