【问题标题】:Get multiple image from variable by ajax通过ajax从变量中获取多个图像
【发布时间】:2021-08-15 16:57:35
【问题描述】:

我正在尝试从变量中获取多个图像有多个图像但我​​只得到一个。

变量是 $src。

//前端代码

              foreach ( $chapter['storage'][ $in_use ]['page'] as $page => $link ) {
                $host = $chapter['storage'][ $in_use ]['host'];
                $src  = $host . $link['src'];

                }
                $this->send_json( 'success', '', $src );

//ajax代码

$(function(){
$('.entry-content .entry-content_wrap').ready(function(){
    var navigation = $('.single-chapter-select').find('option:selected').data('navigation');
    $.ajax({
        type: 'GET',

        url: manga.ajax_url + '?' + navigation,
        dataType: 'json',
        success: function(data){
            var imag_link = data.data.data;
                console.log(imag_link);
                $('.entry-content .entry-content_wrap').html('<img style="margin: -6px;width: 89%;pointer-events: none;" class="wp-manga-chapter-img" src="' + imag_link + '">');
        },
       })
    })
  })

【问题讨论】:

  • $src 将在迭代中被新值替换
  • 我怎样才能从循环中获取所有图像到 ajax
  • 循环开始前创建一个空数组,循环时不断添加$src。然后在 send() 函数中返回数组。将 JavaScript 修改为也循环
  • 我在console.log(value)中得到循环的值;但是当我尝试为 $('.entry-content_wrap').html(''); 添加价值时我得到一张图片

标签: php ajax wordpress


【解决方案1】:

在您的 PHP 代码中,您需要将所有 URL 收集到一个数组中并将其返回。

$sources = [];
foreach ( $chapter['storage'][ $in_use ]['page'] as $page => $link ) {
    $host = $chapter['storage'][ $in_use ]['host'];
    $src  = $host . $link['src'];
    $sources[] = $src;
}
$this->send_json( 'success', '', $sources );

在前端,您应该循环访问源代码并根据需要将它们附加到您的 html 中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-31
    • 2018-09-03
    • 1970-01-01
    • 2016-12-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多