来自javascript的Nan结果

时间:2013-03-30 11:52:54

标签: javascript

嗨,这是我的第一篇文章,也是我第一次尝试使用javascript(我正在使用php)。问题是我从后面的js得到了NaN结果(如果我能得到代码就好了嫁接!)

<script type="text/javascript">
    function Running_Total() { 
        alert('firing');
        var cost1 = "'.$_SESSION['75gmcost'].'"+""; 
        var cost2 = "'.$_SESSION['100gmcost'].'"+""; 
        var cost3 = "'.$_SESSION['1kgcost'].'";+""; 
        var cost4 = "'.$_SESSION['c75gmcost'].'"+""; 
        var qty1 = "'.$_SESSION['qty75gm'].'"+""; 
        var qty2 = "'.$_SESSION['qty100gm'].'"+""; 
        var qty3 = "'.$_SESSION['qty1kg'].'"+""; 
        var qty4 = "'.$_SESSION['qtyc75gm'].'"+""; 
        var sum = 0;
        sum += cost1 * qty1;
        sum += cost2 * qty2;
        sum += cost3 * qty3;
        sum += cost4 * qty4;
        $('#tot_cost').empty();
        $('#tot_cost').append(sum);
    }
</script>

我试图从表单输入中总结订单总数,但是对js不太了解解决Nan问题。 我想我的php变量必须改为适合js,但还不知道该怎么办? ##

2 个答案:

答案 0 :(得分:1)

尝试使用以下代码替换代码中的var分配:

var cost1 = {$_SESSION['75gmcost']};
var cost2 = {$_SESSION['100gmcost']};
var cost3 = {$_SESSION['1kgcost']}; 
var cost4 = {$_SESSION['c75gmcost']}; 
var qty1 = {$_SESSION['qty75gm']};
var qty2 = {$_SESSION['qty100gm']}; 
var qty3 = {$_SESSION['qty1kg']};
var qty4 = {$_SESSION['qtyc75gm']}; 

汇总字符串会导致JavaScript中的值不被解释为数字(NaN)。

这是一个提醒'4'

的工作示例
<?php
    session_start();
    $_SESSION['75gmcost'] = 1; 
    $_SESSION['100gmcost'] = 1;  
    $_SESSION['1kgcost'] = 1; 
    $_SESSION['c75gmcost'] = 1; 
    $_SESSION['qty75gm'] = 1;  
    $_SESSION['qty100gm'] = 1; 
    $_SESSION['qty1kg'] = 1; 
    $_SESSION['qtyc75gm'] = 1; 

    print <<< HERE
           <script type="text/javascript">
    function Running_Total() { 
            alert('firing');
        var cost1 = {$_SESSION['75gmcost']}; 
        var cost2 = {$_SESSION['100gmcost']};
        var cost3 = {$_SESSION['1kgcost']};
        var cost4 = {$_SESSION['c75gmcost']}; 
        var qty1 = {$_SESSION['qty75gm']};
        var qty2 = {$_SESSION['qty100gm']}; 
        var qty3 = {$_SESSION['qty1kg']};
        var qty4 = {$_SESSION['qtyc75gm']}; 
        var sum = 0;
        sum += cost1 * qty1;
        sum += cost2 * qty2;
        sum += cost3 * qty3;
        sum += cost4 * qty4;
        alert(sum);
    }
    Running_Total();
    </script>
    HERE;
?>

答案 1 :(得分:0)

我通常做这样的事情(你可以在这里看到结果:http://jorgecardoso.eu/temp/test.php):

<html>
<head>

<?php
     // just to test
session_start();
$_SESSION['75gmcost'] = 1;
$_SESSION['100gmcost'] = 1;
$_SESSION['1kgcost'] = 1;
$_SESSION['c75gmcost'] = 1;
$_SESSION['qty75gm'] = 1;
$_SESSION['qty100gm'] = 1;
$_SESSION['qty1kg'] = 1;
$_SESSION['qtyc75gm'] = 1;
?>

<script type="text/javascript">
    function Running_Total() {
        alert('firing');
        var cost1 = <?php echo $_SESSION['75gmcost'] ?>;
        var cost2 = <?php echo $_SESSION['100gmcost'] ?>;
        var cost3 = <?php echo $_SESSION['1kgcost'] ?>;
        var cost4 = <?php echo $_SESSION['c75gmcost'] ?>;
        var qty1 =  <?php echo $_SESSION['qty75gm'] ?>;
        var qty2 = <?php echo $_SESSION['qty100gm'] ?>;
        var qty3 = <?php echo $_SESSION['qty1kg'] ?>;
        var qty4 = <?php echo $_SESSION['qtyc75gm'] ?>;
        var sum = 0;
        sum += cost1 * qty1;
        sum += cost2 * qty2;
        sum += cost3 * qty3;
        sum += cost4 * qty4;
        alert( sum);
//        $('#tot_cost').empty();
//        $('#tot_cost').append(sum);
    }
</script>

</head>
<body>
<script type="text/javascript">
Running_Total();
</script>
</body>
</html>