javascript - 使用ajax调用清空div

标签 javascript php jquery html ajax

制作一个ajax,所以当我点击这个Link1按钮时,我需要清空products_list div中的内容

<button type="w3-button">Link1</button>

请帮助我了解如何在单击link1按钮时进行ajax调用,以清空product_list中的产品

下面的代码包含一个javascript来清除products_list的内容,但它不起作用

PHP 文件

 <?php
session_start(); //start session
include("config.inc.php"); //include config file
?>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Stores</title>
<link href="style/style1.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
</head>
<body>
<div align="center">
<h3>Products</h3>
</div>

<script>
$(document).on("click", ".w3-button", function() {
  $('.products-wrp').html('')
 // $("#products_list").html();
});</script>
<?php
//List products from database
$results = $mysqli_conn->query("SELECT product_name, product_desc, product_code, product_image, product_price FROM products_list");
//Display fetched records as you please

$products_list =  '<ul id ="products_list" class="products-wrp">';

while($row = $results->fetch_assoc()) {
$products_list .= <<<EOT
<li>
<form class="form-item">
<h4>{$row["product_name"]}</h4>
<div>
<img src="images/{$row["product_image"]}" height="62" width="62">
</div>
<div>Price : {$currency} {$row["product_price"]}<div>
</form>
</li>
EOT;

}
$products_list .= '</ul></div>';
echo $products_list;
?>
</body>
</html>

最佳答案

检查下面的代码,该代码将在 Ajax 请求失败时清空 div -

function getFailOutput() {
    $.ajax({
        url:'myAjax.php',
        success: function (response) {
            console.log(data, response);
            $('#output').html(response);
        },
        error: function () {
            //Empty Output  Div ON Error Returned From Ajax Request
            $('#output').html('');
        },
    });
    return false;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#" onclick="return getSuccessOutput();"> test success </a> | <a href="#" onclick="return getFailOutput(); return false;"> test failure</a>
<div id="output">waiting for action</div>

关于javascript - 使用ajax调用清空div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44097690/

相关文章:

javascript - ASP.NET MVC 客户端时间从模型转换

javascript - 如何将 bootstrap 年历与 mysql 库连接

php - 在 INI 文件中使用关键字作为变量名

javascript - 使子容器相对于父 div 固定(高度不固定)

javascript - 拉拉维尔{!! Form::open() !!} 在 AngularJS 中不起作用

Javascript 'this' 返回未定义的内部对象

javascript - 从给定的 html 中剥离图像 src

php - 从 PHP 脚本执行 ffmpeg 命令

javascript - Accordion 的滚动相关代码有一些冲突

javascript - 事件类不显示