gpt4 book ai didi

javascript - AJAX - 不返回值

转载 作者:行者123 更新时间:2023-11-28 08:15:03 24 4
gpt4 key购买 nike

我正在尝试显示从 images.phpindex.php 的 mysql 结果。单击我的类别的一个链接(由 id 命名)后,我想显示来自 images.php 的值。
每个类别都有一个<div>id标记
该脚本运行不佳,仅显示正在加载...

$("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("");
});

谢谢

[编辑]

这是我在 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>

这是 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
}
?>

最佳答案

试试这个:

$("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...");
}
});
});

关于javascript - AJAX - 不返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23703113/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com