简体   繁体   English

未捕获的 ReferenceError:getProduct 未在 HTMLButtonElement.onclick 中定义

[英]Uncaught ReferenceError: getProduct is not defined at HTMLButtonElement.onclick

I am building an E-commerce website.我正在建立一个电子商务网站。

My problem is that when a customer clicks on a product, how do I save all the data of that product (id, name, price) in a javascript object and then send the array of select product objects to a PHP file using Ajax?我的问题是,当客户点击某个产品时,如何将该产品的所有数据(id、名称、价格)保存在 javascript 对象中,然后使用 Ajax 将选定产品对象的数组发送到 PHP 文件?

this is my current implementation:这是我目前的实现:

  <?php 
while($row=mysqli_fetch_assoc($product))
{
    $id=$row['id'];
    $name=$row['name'];
    $price=$row['price'];
?>
<div class="product" onclick="getProduct(this)">
    <p data-pid="<?php echo $id ?>" ><?php echo $id ?></p>
    <p data-pname="<?php echo $name ?>"><?php echo $name ?></p>
    <div class="product-footer">
        <p data-pprice="<?php echo $price ?>"><?php echo $price ?></p>
        <button>Add To Cart</button>
    </div>
</div>
<?php
}
?>

JS Code: JS代码:

var products = new Array();
var product= new Object();
function getProduct(obj) {
     product.id = $(obj+' [data-pid]').data('pid');
     product.name= $(obj+' [data-pname]').data('pname');
     product.price= $(obj+' [data-pprice]').data('price');
     products.push(product);
     console.log(products);
}

this implementation is not working I get this javsscript error此实现不起作用我收到此 javsscript 错误

Uncaught ReferenceError: getProduct is not defined at HTMLButtonElement.onclick未捕获的 ReferenceError:getProduct 未在 HTMLButtonElement.onclick 中定义

just do this就这样做

     <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> //Jquery Plugin

     <?php 
        while($row=mysqli_fetch_assoc($product)){echo '
        <div class="product" data-id="'.$row['id'].'" data-name="'.$row['name'].'" data-price="'.$row['price'].'">
            <p>'.$row['id'].'</p>
            <p>'.$row['name'].'</p>
            <div class="product-footer"><p>'.$row['price'].'</p><button>Add To Cart</button></div>
        </div>';
        }
    ?>
    <script>
    $('.product').click(function(e) {
        product = {'id':$(this).data('id'),'name':$(this).data('name'),'price':$(this).data('price')};
        console.log(product);
    });
    </script>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获的ReferenceError:在HTMLButtonElement.onclick中未定义函数 - Uncaught ReferenceError: function is not defined at HTMLButtonElement.onclick 未捕获的ReferenceError:未在HTMLButtonElement.onclick上定义toggleFunction - Uncaught ReferenceError: toggleFunction is not defined at HTMLButtonElement.onclick 未捕获的ReferenceError:未在HTMLButtonElement.onclick上定义测试 - Uncaught ReferenceError: testing is not defined at HTMLButtonElement.onclick 未捕获的 ReferenceError:异步未在 HTMLButtonElement.onclick 中定义 - Uncaught ReferenceError: async is not defined at HTMLButtonElement.onclick Uncaught ReferenceError: init is not defined at HTMLButtonElement.onclick - Uncaught ReferenceError: init is not defined at HTMLButtonElement.onclick 未捕获的ReferenceError :(函数)未在HTMLButtonElement.onclick中定义 - Uncaught ReferenceError: (function) not defined at HTMLButtonElement.onclick 未捕获的ReferenceError:未在HTMLButtonElement.onclick中定义查找 - Uncaught ReferenceError: lookup is not defined at HTMLButtonElement.onclick 未捕获的 ReferenceError:showCurrentPage 未在 HTMLButtonElement.onclick 中定义 - Uncaught ReferenceError: showCurrentPage is not defined at HTMLButtonElement.onclick 未捕获的 ReferenceError:在 HTMLButtonElement.onclick 中未定义 postAcmgForm - Uncaught ReferenceError: postAcmgForm is not defined at HTMLButtonElement.onclick 未捕获的ReferenceError:HTMLButtonElement.onclick中未定义submitToAPI - Uncaught ReferenceError: submitToAPI is not defined at HTMLButtonElement.onclick
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM