gpt4 book ai didi

javascript - Nan 来自 javascript 的结果

转载 作者:行者123 更新时间:2023-11-28 02:20:34 24 4
gpt4 key购买 nike

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

最佳答案

尝试用以下内容替换代码中的 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;
?>

关于javascript - Nan 来自 javascript 的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15718218/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com