【问题标题】:jquery calculate the total amount but do not show itjquery计算总金额但不显示
【发布时间】:2016-09-18 09:01:20
【问题描述】:

我正在尝试建立一个小型电子商务网站,现在我刚刚让我的 jquery 程序计算购物车上的物品,我还试图计算这些物品的总量并将其打印在第一个旁边.因此,当我检查我的源代码然后联网然后我在谷歌浏览器中的 js 文件时,我看到金额计算正确但未显示。这是我的代码:

我的html跨度:(在哪里显示总金额):

<span id="total12"><?php echo $_SESSION['total'];?></span>

我的 addpanier.php 文件:

<?php 
    session_start();
    require 'connexion.php';
    require 'panier.php';
    $json=array('error' => true);
    $panier=new panier();
    try {
        $con1 = new myPDO();
    }
    catch (PDOException $e) {
        echo "<br/> Erreur: ".$e->getMessage()."<br/>" ;
        die() ;
    }
    if (isset($_GET['id'])) {
        $bdd = $con1->prepare("SELECT * FROM article a,promotion p WHERE a.id=p.id_article HAVING a.id=?") ;
        $bdd->bindValue(1,$_GET["id"]);
        $bdd->execute();
        $result = $bdd->fetch(PDO::FETCH_ASSOC);
        $tot2=0;
        if (empty($result)) {
            $json['message']="produit introuvable!";
        }else{
            $panier->add($result["id"]);
            $json['error']= false;
            if (isset($_SESSION['panier'])) {
                $tot=0;
                $tot=$result["prix"]*$_SESSION['panier'][$result["id"]];
                $b=$tot-$tot*($result["rabais"]/100)+$result["tax"];
                $tot2+=$b;
            }else{
                $tot=0;
                $tot=$result["prix"];
                $b=$tot-$tot*($result["rabais"]/100)+$result["tax"];
                $tot2+=$b;
            }
            $json['total1']=$tot2;
            $json['panier']= array_sum($_SESSION['panier']);
            $json['message']='produit bien ajoute a votre panier !';
        }
    }else{
        $json['message']="pas de produits a ajouter au panier";
    }
    echo json_encode($json);
?>

还有我的 jquery 文件:

(function($){
    $('.addpanier').click(function(event){
        event.preventDefault();
        $.get($(this).attr('href'),{},function(data){
            if (data.error) {
                alert(data.message);
            }else{
                if(confirm(data.message + '. Voulez vous consulter votre panier?')){
                    location.href="product_summary.php";
                }else{
                    $('#panier12').empty().append(data.panier);
                    $('#total12').empty().append(data.total1);
                }
            }
        },'json');
        return false;
    });
})(jQuery);

【问题讨论】:

    标签: javascript php jquery ajax e-commerce


    【解决方案1】:

    谢谢大家的时间,我的错误出现在我的选择器中,事实上,我正在检查会话变量是否存在,就在我应该显示我的东西之前我最好上床睡觉......(现在是摩洛哥凌晨 2 点)。 :D 谢谢大家!

    【讨论】:

      【解决方案2】:

      使用 .html()

       $('#total12').html(data.total1);
      

      【讨论】:

      • 好吧,那么选择器可能有问题或者值未定义,像这样调试它有点困难,但如果你尝试从控制台更新一个值,你会看到它将更新它...
      • 打开开发者控制台并检查你是否到达了那段代码
      • 是的,它到达了那个代码块,因为 $('#panier12').empty().append(data.panier);工作正常,我也可以在那里看到计算出的金额,但它不会显示...
      • 并且没有错误出现? $('#total12') 是否返回元素并且是否填充了 data.total1?
      • 这是 jquery 文件返回的内容:{error: false, total1: 4075, panier: 1, message: "produit bien ajoute a votre panier !"} total1 被计算出来!但不会显示..
      猜你喜欢
      • 2023-04-07
      • 2015-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      相关资源
      最近更新 更多