1

嗨,这是我的第一篇文章,也是我第一次尝试使用 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,但不知道该怎么做?##

4

2 回答 2

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;
?>
于 2013-03-30T12:06:34.140 回答
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>
于 2013-03-30T13:03:28.510 回答