Regarding the delegate problem of jq
<code>//PHP <?php include "db.php"; if(isset($_POST["category"])){ $category_query = "SELECT * FROM categories"; $run_query = mysqli_query($con,$category_query); echo " <div class='nav nav-pills nav-stacked'> <li class='active'><a href='#'><h4>Categories</h4></a></li> "; if(mysqli_num_rows($run_query)>0){ while($row = mysqli_fetch_array($run_query)){ $cid = $row["cat_id"]; $cat_name = $row["cat_title"]; //问题在这里 echo " <li><a href='#' class='category' cid='$cid'>$cat_name</a></li> "; } echo "</div>"; } } if(isset($_POST["brand"])){ $brand_query = "SELECT * FROM brands"; $run_query = mysqli_query($con,$brand_query); echo " <div class='nav nav-pills nav-stacked'> <li class='active'><a href='#'><h4>Brands</h4></a></li> "; if(mysqli_num_rows($run_query)>0){ while($row = mysqli_fetch_array($run_query)){ $bid = $row["brand_id"]; $brand_name = $row["brand_title"]; echo " <li><a href='#'>$brand_name</a></li> "; } echo "</div>"; } } if(isset($_POST["getProduct"])){ $product_query = "SELECT * FROM products ORDER BY RAND() LIMIT 0,9"; $run_query = mysqli_query($con,$product_query); if(mysqli_num_rows($run_query) >0 ){ 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 " <div class='col-md-4'> <div class='panel panel-info'> <div class='panel-heading'>$pro_title</div> <div class='panel-body'> <img style='width:300px;height:300px' src='product_image/$pro_image' /> </div> <div class='panel-heading'>$.$pro_price.00 <button pid='$pro_id' style='float:right' class='btn btn-danger btn-xs'>AddToCart</button> </div> </div> </div> "; } } } ?></code>
<code>//JS $(document).ready(function() { cat(); brand(); product(); function cat() { $.ajax({ url: 'action.php', type: 'POST', data: { category: 1 }, }) .done(function(data) { //console.log(data); $("#get_category").html(data); }); } function brand() { $.ajax({ url: 'action.php', type: 'POST', data: { brand: 1 }, }) .done(function(data) { //console.log(data); $("#get_brand").html(data); }); } function product() { $.ajax({ url: 'action.php', type: 'POST', data: { getProduct: 1 }, }) .done(function(data) { //console.log(data); $("#get_product").html(data); }); } //使用 delegate() 方法。on不能获取 $("body").delegate(".category", "click", function() { alert(123); }) // $(".category").on("click", function(event) { // alert(13); // }); })</code>
Why is there no response when clicking when I use the on method? But the delegate does? Is it because this class is written in PHP and cannot be read by on? Or what?
Reply content:
<code>//PHP <?php include "db.php"; if(isset($_POST["category"])){ $category_query = "SELECT * FROM categories"; $run_query = mysqli_query($con,$category_query); echo " <div class='nav nav-pills nav-stacked'> <li class='active'><a href='#'><h4>Categories</h4></a></li> "; if(mysqli_num_rows($run_query)>0){ while($row = mysqli_fetch_array($run_query)){ $cid = $row["cat_id"]; $cat_name = $row["cat_title"]; //问题在这里 echo " <li><a href='#' class='category' cid='$cid'>$cat_name</a></li> "; } echo "</div>"; } } if(isset($_POST["brand"])){ $brand_query = "SELECT * FROM brands"; $run_query = mysqli_query($con,$brand_query); echo " <div class='nav nav-pills nav-stacked'> <li class='active'><a href='#'><h4>Brands</h4></a></li> "; if(mysqli_num_rows($run_query)>0){ while($row = mysqli_fetch_array($run_query)){ $bid = $row["brand_id"]; $brand_name = $row["brand_title"]; echo " <li><a href='#'>$brand_name</a></li> "; } echo "</div>"; } } if(isset($_POST["getProduct"])){ $product_query = "SELECT * FROM products ORDER BY RAND() LIMIT 0,9"; $run_query = mysqli_query($con,$product_query); if(mysqli_num_rows($run_query) >0 ){ 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 " <div class='col-md-4'> <div class='panel panel-info'> <div class='panel-heading'>$pro_title</div> <div class='panel-body'> <img style='width:300px;height:300px' src='product_image/$pro_image' /> </div> <div class='panel-heading'>$.$pro_price.00 <button pid='$pro_id' style='float:right' class='btn btn-danger btn-xs'>AddToCart</button> </div> </div> </div> "; } } } ?></code>
<code>//JS $(document).ready(function() { cat(); brand(); product(); function cat() { $.ajax({ url: 'action.php', type: 'POST', data: { category: 1 }, }) .done(function(data) { //console.log(data); $("#get_category").html(data); }); } function brand() { $.ajax({ url: 'action.php', type: 'POST', data: { brand: 1 }, }) .done(function(data) { //console.log(data); $("#get_brand").html(data); }); } function product() { $.ajax({ url: 'action.php', type: 'POST', data: { getProduct: 1 }, }) .done(function(data) { //console.log(data); $("#get_product").html(data); }); } //使用 delegate() 方法。on不能获取 $("body").delegate(".category", "click", function() { alert(123); }) // $(".category").on("click", function(event) { // alert(13); // }); })</code>
Why is there no response when I click on the on method? But delegate does? Is it because this class is written in PHP and cannot be read by on? Or what?
Use on
for dynamically generated elements<code> $('body').on('click','.category' , function() { alert(123); })</code>
Before you use a new API, you should fully understand its function through the documentation.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.
