IE浏览器中的Javascript NaN但在Chrome,Firefox中运行良好

时间:2012-03-24 21:31:37

标签: javascript html

我有以下javascript代码在firefox和chrome中正常工作。但是当我在IE中加载这个页面时,所有字段都被NaN填充。

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>

1 个答案:

答案 0 :(得分:0)

不确定这一点,但它可能值得一试。而不是:

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

尝试:

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

或者:

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

理论上,如果存在冲突类型,+运算符将优先考虑其字符串连接行为(请参阅this post以获得一些示例),但使用MSIE,您唯一能做的就是测试。 ..很多。

祝你好运;)