单击电子商务网站中的搜索按钮后,如何阻止页面重新加载?

任何帮助将非常感激?


问题是搜索按钮完成了它的功能。该页面应该只显示与产品关键字匹配的产品,它只显示与相关关键字匹配的产品,但它会显示 0.1 毫秒,然后重新加载以显示整个页面。但是,如果我只单击一个类别选项卡,即床/沙发(在我的情况下)。它只是向我显示床或沙发的产品,不会重新加载。这与我想要的搜索按钮相同。编辑:我已经尝试过preventDefault,但它不起作用请参阅下面的相关代码:


索引.php


<form class="navbar-form navbar-left">

                <div class="form-group">

                  <input type="text" class="form-control" placeholder="Search" id="search">

                </div>

                <button type="submit" class="btn btn-primary" id="search_btn"><span class="glyphicon glyphicon-search"></span></button>

</form>

动作.php


if(isset($_POST["get_seleted_Category"]) || isset($_POST["selectBrand"]) || isset($_POST["search"])){

    if(isset($_POST["get_seleted_Category"])){

        $id = $_POST["cat_id"];

        $sql = "SELECT * FROM products WHERE product_cat = '$id'";

    }else if(isset($_POST["selectBrand"])){

        $id = $_POST["brand_id"];

        $sql = "SELECT * FROM products WHERE product_brand = '$id'";

    }else {

        $keyword = $_POST["keyword"];

        $sql = "SELECT * FROM products WHERE product_keywords LIKE '%$keyword%'";

    }


    $run_query = mysqli_query($con,$sql);

    while($row=mysqli_fetch_array($run_query)){

            $pro_id    = $row['product_id'];

            $pro_cat   = $row['product_cat'];

            $pro_brand = $row['product_brand'];

            $pro_title = $row['product_title'];

            $pro_price = $row['product_price'];

            $pro_image = $row['product_image'];

            echo "



桃花长相依
浏览 128回答 2
2回答

ABOUTYOU

确保执行阻止默认设置并将按钮设置为禁用,因此用户无法再次单击,在响应收到时将按钮设置为启用$("#search_btn").click(function(e){&nbsp; &nbsp; e.preventDefault();&nbsp; &nbsp; $("#search_btn").attr("disabled", true);&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; $("#get_product").html("<h3>Loading...</h3>");&nbsp; &nbsp; var keyword = $("#search").val();&nbsp; &nbsp; if(keyword != ""){&nbsp; &nbsp; &nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; url&nbsp; &nbsp; &nbsp;:&nbsp; &nbsp;"action.php",&nbsp; &nbsp; &nbsp; &nbsp; method&nbsp; :&nbsp; &nbsp;"POST",&nbsp; &nbsp; &nbsp; &nbsp; data&nbsp; &nbsp; :&nbsp; &nbsp;{search:1,keyword:keyword},&nbsp; &nbsp; &nbsp; &nbsp; success :&nbsp; &nbsp;function(data){&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $("#get_product").html(data);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($("body").width() < 480){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $("body").scrollTop(683);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $("#search_btn").attr("disabled", false);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; })&nbsp; &nbsp; }})

拉风的咖菲猫

您提到按钮类型为“提交”。将其设为“按钮”。&nbsp;<button type="button" class="btn btn-primary"&nbsp;&nbsp; id="search_btn"><span class="glyphicon glyphicon-&nbsp;&nbsp; search"></span></button>
打开App,查看更多内容
随时随地看视频慕课网APP