首页 > 解决方案 > 为什么我的 jquery 调用刷新页面?

问题描述

$(document).ready(function(){
    $(document).on("click","#btn_delete",function () {
        console.log('working');
            });
});
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>HomeShopping</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" >
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
<div class="container-fluid">
    <div class="row px-5">
        <div class="col-md-7">
            <div class="shopping-cart">
                <h6>My Cart</h6>
                <hr>
             <?php
                include("functions/functions.php");

                $total = 0;

                $ip_add = getRealIpUser();

                $select_cart = "select * from cart where ip_add='$ip_add'";

                $run_cart = mysqli_query($conn,$select_cart);

                while($row_cart = mysqli_fetch_array($run_cart)){

                    $pro_id = $row_cart['p_id'];

                    $pro_size = $row_cart['size'];

                    $pro_quantity = $row_cart['quantity'];

                    $get_products = "select * from products where no='$pro_id'";

                    $run_products = mysqli_query($conn,$get_products);

                    while($row_products = mysqli_fetch_array($run_products)){

                        $product_title = $row_products['product_title'];

                        $product_img1 = $row_products['product_img1'];

                        $only_price = $row_products['product_price'];


                        ?>
                        <form  method="post" class="cart-items">
                            <div class="border rounded">
                                <div class="row bg-white">
                                    <div class="col-md-3 pl-0">
                                        <img src="product_images/<?php echo $product_img1; ?>" alt="Image1" class="img-fluid">
                                    </div>

                                    <div class="col-md-6">
                                        <h5 class="pt-2"><?php echo $product_title; ?></h5>
                                        <small class="text-secondary">Seller: dailytuition</small>
                                        <h5 class="pt-2"><?php echo $only_price; ?></h5>
                                        <button type="submit" class="btn btn-warning">Save for Later</button>
                                        <button id="btn_delete" class="btn btn-danger mx-2"  value="<?php echo $pro_id;?>">Remove</button>
                                    </div>

                                    <div class="col-md-3 py-5">
                                        <button type="button" class="btn bg-light border rounded-circle" ><i class="fas fa fa-minus" ></i></button>
                                        <input type="text" id="<?php echo $pro_id?>" value="<?php echo $pro_quantity?>" class="form-controls w-25 d-inline">
                                        <button type="button" class="btn bg-light border rounded-circle" > <i class="fas fa fa-plus" ></i></button>
                                    </div>
                                </div>
                            </div>
                        </form>


                    <?php }}?>
            </div>
        </div>
    </div>
</div>

1.连接数据库,我的代码没有问题

我的问题是当我检查我的 jquery 代码时,console.log('working') 它没有显示在我的 F12 中

控制台照片

2.另外,我添加了其他jquery代码来检查我的问题

$(document).ready(function(){
    $(document).on("click","#remove",function () {

        let id = $(this).attr("value");
        console.log(id);
 }
}

执行以上代码,点击remove按钮刷新页面。有什么问题?

标签: phpjquery

解决方案


推荐阅读