如何使用javascript将图像附加到选择选项中

时间:2021-11-24 02:06:35

please i need help in appending image src into select option in javascript

我需要帮助将图像src附加到javascript中的选择选项

here is my code :

这是我的代码:

 $.ajax({
       type: "POST",
       url: "http://10.102.220.119:8001/api/1/device/devices",
       dataType: "json",
       data: { ids : ids}, 
       success:function(data){
        for (var i = 0 ; i<data.length;i++){
            var image = data[i].image;
            var mime = data[i].mime;

            var img = new Image();
            img.src = "data:"+mime+";base64,"+image;
            document.body.appendChild(img);

            /*var option = document.createElement("option");
            option.data-img-src = img.src;
            var select = document.getElementById("selectImage");
            select.appendChild(option);*/
        }

    }

 });

i use

我用

option.data-img-src = img.src; 

but it display this error :

但它显示此错误:

Invalid left-hand side in assignment 

please any help

请任何帮助

2 个解决方案

#1


1  

When the key of an object contains - you can use option["data-img-src"] instead

当对象的键包含时 - 您可以使用选项[“data-img-src”]代替

#2


1  

Try (pure javascript):

试试(纯javascript):

option.setAttribute('data-img-src', img.src);

#1


1  

When the key of an object contains - you can use option["data-img-src"] instead

当对象的键包含时 - 您可以使用选项[“data-img-src”]代替

#2


1  

Try (pure javascript):

试试(纯javascript):

option.setAttribute('data-img-src', img.src);