简体   繁体   English

AJAX-不返回值

[英]AJAX - doesn't return value

I am trying to show mysql results from images.php to index.php . 我试图显示从images.phpindex.php的 mysql结果。 After I click on one link of my categories(which are named by id ) then I want to show values from images.php . 在单击类别的一个链接(以id命名)后,我想显示images.php中的值。
Every category has one <div> which is tagged by id 每个类别都有一个<div> ,并用id标记
The script doesn't work well, it only shows Loading... 该脚本无法正常运行,仅显示正在加载...

$("a.load").on('click',function() {
 var id = $(this).attr('data-gallery'); 
  $.ajax({
   url: 'http://example.com/images.php?c=' + id,
   beforeSend: function() {
   $("#" + id).html("Loading...");
  },
  success: function(data) {
   $("#" + id).html(data);
  }
 });
},function() {
   var id = $(this).attr('data-gallery'); 
    $("#" + id).html("");
});  

Thanks 谢谢

[EDIT] [编辑]

Here is my HTML code in index.php 这是我在index.php中的 HTML代码

 <section class="mainmywork" id="categories">
    <div class="container">
    <!--<h1 class="myworkheading"><p>CATEGORIES</p></h1>!-->        
    <?php
     foreach($images as $categories) {
      echo '<div class="col-lg-4 col-md-6 col-sm-6 col-xs-12 categories">
             <a class="load" data-gallery="'.$categories["code"].'" style="cursor: pointer;"><img class="center-block" src="'.showConfigValues("img_source").'/'.$categories["photos_name"].'" alt=""></a>
              <a class="load" data-gallery="'.$categories["code"].'" style="cursor: pointer;"><h2><p>'.$categories["name"].'</p></h2></a>
            </div>
            <div id="'.$categories["code"].'">

            </div>';
     }
    ?>
    </div>
   </section>

and here is the which is in images.php 这是images.php中的

<?php
 if(isset($_GET["c"])) {
  $gallery_code = htmlspecialchars($_GET["c"]);

  require_once 'init.php';

 $db = new DB;

  $query = "SELECT * 
       FROM photos
       INNER JOIN gallery
        ON gallery.code = photos.gallery_code AND photos.title_photo != 'y'
       WHERE photos.gallery_code = '".$gallery_code."' OR gallery.name = '".$gallery_code."'
        ORDER BY photos.id"; 

     $photos = $db->get_special($query); 
  ?>

  <div id="lk-gallery">
   <div class="wrapper">
    <main id="main" role="main" class="photo-lk">
     <?php
   if($photos[0] != '') {
    foreach($photos as $image) {
     $mask_description = $image["photos_description"];
      echo '<img src="'.showConfigValues("img_source").'/'.$image["photos_name"].'" />'; 
    }    
   } 
    else 
         { echo '<p class="inprogress">Sorry, I am working on this one!<br><a href="/#categoriesanchor">Check out my other photos</a></p>'; } 
    ?>
    </main>
   </div>
  </div>

  <div class="lk-gallery-touch">
   <div class="flexslider">
    <div class="wrapper">
     <main id="main" role="main" class="photo-lk">
   <ul class="slides">
    <?php
     if($photos[0] != '') {
      foreach($photos as $image) {
       $mask_description = $image["photos_description"];
        echo '<li><img src="'.showConfigValues("img_source").'/'.$image["photos_name"].'" /></li>'; 
      }    
     } 
      else 
          { echo '<p class="inprogress">Sorry, I am working on this one!<br><a href="/#categoriesanchor">Check out my other photos</a></p>'; } 
    ?>
     </ul> 
    </main>
   </div> 
  </div>
 </div>
 <?php 
  }
  ?>

Try this: 尝试这个:

$("a.load").on('click',function() {
  var id = $(this).data('gallery'); // use .data() method 
  var $id = '#'+id;
  $.ajax({
      url: 'http://example.com/images.php?c=' + id,
      beforeSend: function() {
          $id.html("Loading...");
      },
      success: function(data) {
         $id.html(data);
      },
      error: function(){
         $id.html("Error...");
      }
  });
}); 

According to the documentation , the second argument to the .on function when there is more than 2 arguments is a selector string, but you have passed what appears to be your desired event handler. 根据文档 ,当有两个以上参数时, .on函数的第二个参数是选择器字符串,但是您已经传递了似乎是所需事件处理程序的参数。 The last argument should be the event handler, in your code sample that is: 在您的代码示例中,最后一个参数应该是事件处理程序,即:

function() {
   var id = $(this).attr('data-gallery'); 
   $("#" + id).html("");

If you remove that last function, leaving just 2 arguments to .on , your handler should be called as expected. 如果删除最后一个函数,而仅在.on保留2个参数,则.on预期方式调用处理程序。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM