如何将图像附加到特定的班级

时间:2017-04-02 16:34:52

标签: javascript jquery html ajax

我有多个具有相同标记的类(.offer)我正在循环并将图像添加到我的所有.offer div中。我只想添加特定于发布用户的图像。我怎样才能做到这一点?假设后端工作正常

jQuery (第一个功能)

    function getOffers(key) {
            dict = {
                'key': key// pk value of post sent to retrieve offers to it
            };

            generateCSRFToken();
            $.ajax({
                url: "/retrieve_offers/",
                method: "POST",
                data: JSON.stringify(dict),
                success: function (data) {
                    data = JSON.parse(data);

                    console.log(appendUserImage(38));
                    $("#offercontainer").empty();
                    $(".offer").empty();
                    for (var i = 0; i < data.length; i++) {

                        var string = data[i].fields.author_name;
                        $("#offercontainer").append(
                            "<div class='offer'>" +
                            "<p class=offername>" + string + "</p>" +
                            "<p class=offertext> offered his " + " " + data[i].fields.item_name +  "</p>" +
                            "</div>"
                        );
                        appendUserImage(data[i].fields.author);
                    }
                },
                error: function () {
                }

            })
        }

jQuery (第二功能)

  function appendUserImage(key) {
        dict = {
            'key': key// pk value of post sent to retrieve offers to it
        };
        generateCSRFToken();
        $.ajax({
            url: "/get_user/",
            method: "POST",
            data: JSON.stringify(dict),
            success: function (data) {
                $('<img />', {
                    src: data["image"],
                    class: "offer_user_image"
                }).appendTo($('.offer'))

            },
            error: function () {
            }

        });

    }

我只是想将图片附加到各自的提议div帮助

2 个答案:

答案 0 :(得分:0)

您可以传递给appendUserImage div getOffers函数创建或执行类似的操作,为创建的.offer容器添加一个唯一的键选择器,并在appendUserImage函数中将其作为目标。

function getOffers(key) {

中的

$("#offercontainer").append(
  "<div class='offer' data-author-key='" + data[i].fields.author +"'>" +
  "<p class=offername>" + string + "</p>" +
  "<p class=offertext> offered his " + " " + data[i].fields.item_name +  "</p>" +
  "</div>");
  appendUserImage(data[i].fields.author);
function appendUserImage(key) {

中的

$('<img />', {
 src: data["image"],
 class: "offer_user_image"
 }).appendTo($('.offer[data-author-key="' + key + '"]'))

答案 1 :(得分:0)

感谢那些尝试回答的人,我设法通过使用第n个孩子来解决我的问题,并且这样做:

= 'unconf' at line 5

第二功能:

DELIMITER //
CREATE PROCEDURE check_for_days()
BEGIN 
    SET @i = (SELECT COUNT(*) FROM `ph` WHERE `payment_status` = 'unconfirmed' && TIMESTAMPDIFF(DAY, `date_paired`, NOW()) > 2) ;

    label1:WHILE @i>0
        SET @amnt = (SELECT `initial_payment` FROM `ph` WHERE `payment_status` = 'unconfirmed' && TIMESTAMPDIFF(DAY, `date_paired`, NOW()) > 2 LIMIT 1);
        SET @id_to_cred = (SELECT `to_pay_id` FROM `ph` WHERE `payment_status` = 'unconfirmed' && TIMESTAMPDIFF(DAY, `date_paired`, NOW()) > 2 LIMIT 1);
        UPDATE `gh` SET `remaining_to_get` = + @amnt WHERE `u_id` = @id_to_cred;
        DELETE FROM `ph` WHERE `payment_status` = 'unconfirmed' && TIMESTAMPDIFF(DAY, `date_paired`, NOW()) > 2 LIMIT 1;
        SET @i = @i - 1;
    END WHILE label1;
END 
DELIMITER ;