【问题标题】:Loop through array and append image src attribute for multiple image place holders from an Ajax Success response从 Ajax 成功响应中循环遍历数组并为多个图像占位符附加图像 src 属性
【发布时间】:2020-10-02 05:00:46
【问题描述】:

我正在尝试遍历一个数组并将 Base64 数据 URL 附加到来自 Ajax Success 响应的多个图像占位符(这些是在 Asp.net Webforms 页面中动态创建的)的图像 src 属性。

这是我尝试过的:

success: function (response) {
    var getData = JSON.parse(response.d);
    for (var x = 0; x < getData.length; x++) {
        uri = getData[x].ImageURI;
        imageID = getData[x].ID;
        $("[id*=ContentPlaceHolder1_Image",{imageID},"]").attr("src", uri);
    }
},

我的图片占位符如下所示:

<img id="ContentPlaceHolder1_Image236" src="The Base64 URL goes here">
<img id="ContentPlaceHolder1_Image237" src="The Base64 URL goes here">
<img id="ContentPlaceHolder1_Image238" src="The Base64 URL goes here">
...

除了设置src 属性外,我还将响应中的图像 ID 附加到此占位符。我在控制台中没有任何错误,但没有填充图像。

【问题讨论】:

    标签: javascript ajax


    【解决方案1】:

    你需要连接字符串来创建它们的选择器

    所以要么使用+ 和字符串

    $("[id*=ContentPlaceHolder1_Image" + imageID + "]").attr("src", uri);
    

    或模板文字(注意不同的引号和$)

    $(`[id*=ContentPlaceHolder1_Image${imageID}]`).attr("src", uri);
    

    【讨论】:

      猜你喜欢
      • 2015-05-09
      • 2018-11-05
      • 2015-01-15
      • 1970-01-01
      • 1970-01-01
      • 2023-01-29
      • 1970-01-01
      • 2013-07-16
      相关资源
      最近更新 更多