如何在选择标签javascript中将图像添加到动态选项

时间:2019-05-04 13:42:10

标签: javascript arrays html5 object

我在JSON中有图片网址,如下所示:

using System;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        dynamic ViewBag = new System.Dynamic.ExpandoObject();

        //Add a single string variable to ViewBag

        ViewBag.xxx = "Stackoverflow";
        Console.WriteLine(ViewBag.xxx);

        //Add a list to ViewBag
        ViewBag.Items = new List<int>() {10,20,30};

        for(var i = 0; i < ViewBag.Items.Count; i++) {
           Console.WriteLine(ViewBag.Items[i]);
        }
    }
}
var obj = [
   {
     country: "SG",
     images: "../images/flag/singapore.jpg"
   },
   {
     country: "TH",
     images: "../images/flag/thailand.jpg"
   }
]


function render() {
 return `
 <select name="sourcecountrycode" id="sourcecountrycode">
      ${obj.map((cn) => `
<option value=${cn.country}>${cn.country}</option>`)} 
// here options SG, TH are generated in select tag dynamically
  </select>
`
}

$('#block').html(render());

如何在使用JavaScript的选择中将图像添加到相应选项?

1 个答案:

答案 0 :(得分:0)

也许是一个入门的地方:我无耻地偷了这个https://www.addwebsolution.com/blog/adding-image-select-list-with-cross-browser-compatibility并试图使其富有活力。它不是很漂亮,但是有点奏效。

jQuery().ready(function() {
  /* Custom select design */
  var obj = [{
      country: "SG",
      images: "https://picsum.photos/25"
    },
    {
      country: "TH",
      images: "https://picsum.photos/25"
    }
  ];
  document.body.innerHTML = `
 <div class="drop-down">    
	   <select name="options">     
	   ${obj.map(cn => `
     <option class="${cn.country}" 
	   style="background-image:url('${cn.images}');" value="${cn.country}">${cn.country}</option> 
     `)}
</select>
</div>
`;

  jQuery('.drop-down').append('<div class="button"></div>');
  jQuery('.drop-down').append('<ul class="select-list"></ul>');
  jQuery('.drop-down select option').each(function() {
    var bg = jQuery(this).css('background-image');
    jQuery('.select-list').append('<li class="clsAnchor"><span value="' + jQuery(this).val() + '" class="' + jQuery(this).attr('class') + '" style=background-image:' + bg + '>' + jQuery(this).text() + '</span></li>');
  });
  jQuery('.drop-down .button').html('<span style=background-image:' + jQuery('.drop-down select').find(':selected').css('background-image') + '>' + jQuery('.drop-down select').find(':selected').text() + '</span>' + '<a href="javascript:void(0);" class="select-list-link">></a>');
  jQuery('.drop-down ul li').each(function() {
    if (jQuery(this).find('span').text() == jQuery('.drop-down select').find(':selected').text()) {
      jQuery(this).addClass('active');
    }
  });
  jQuery('.drop-down .select-list span').on('click', function() {
    var dd_text = jQuery(this).text();
    var dd_img = jQuery(this).css('background-image');
    var dd_val = jQuery(this).attr('value');
    jQuery('.drop-down .button').html('<span style=background-image:' + dd_img + '>' + dd_text + '</span>' + '<a href="javascript:void(0);" class="select-list-link">></a>');
    jQuery('.drop-down .select-list span').parent().removeClass('active');
    jQuery(this).parent().addClass('active');
    $('.drop-down select[name=options]').val(dd_val);
    $('.drop-down .select-list li').slideUp();
  });
  jQuery('.drop-down .button').on('click', 'a.select-list-link', function() {
    jQuery('.drop-down ul li').slideToggle();
  });
  /* End */
});
.drop-down {
  position: relative;
  display: inline-block;
  width: auto;
  margin-top: 0;
}

.drop-down select {
  display: none;
}

.drop-down .select-list {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 1;
  margin-top: 40px;
  padding: 0;
  background-color: #595959;
}

.drop-down .select-list li {
  display: none;
}

.drop-down .select-list li span {
  display: inline-block;
  min-height: 40px;
  min-width: 280px;
  width: 100%;
  padding: 5px 15px 5px 35px;
  background-color: #595959;
  background-position: left 10px center;
  background-repeat: no-repeat;
  font-size: 16px;
  text-align: left;
  color: #FFF;
  opacity: 0.7;
  box-sizing: border-box;
}

.drop-down .select-list li span:hover,
.drop-down .select-list li span:focus {
  opacity: 1;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>