How to add images in select list?

后端 未结 14 1436
小鲜肉
小鲜肉 2020-11-21 11:18

I have a select list of genders.

Code:


                        
    
提交评论

  • 2020-11-21 12:06

    UPDATE: As of 2018, this seems to work now. Tested in Chrome, Firefox, IE and Edge

    <!DOCTYPE html>
    <html>
    <body>
    
    <style>
    select#newlocale option[value="volvo"]   { background-color: powderblue;   }
    select#newlocale option[value="opel"]   { background-color: red;   }
    select#newlocale option[value="audi"]   { background-color: green;   }
    </style> 
    
    <select id="newlocale">
      <option value="volvo"><div >Volvo</div></option>
      <option value="saab">Saab</option>
      <option value="opel">Opel</option>
      <option value="audi">Audi</option>
    </select>
    
    </body>
    </html>
    
    0 讨论(0)
  • 2020-11-21 12:09

    Alvaros JS free answer was a great start for me, and I really tried to get a truly JS-free answer that still delivered all the functionality expected of a Select with images, but sadly nesting forms was the down-fall. I'm posting two solutions here; my main solution that uses 1 line of JavaScript, and a totally JavaScript-free solution that won't work inside another form, but might be useful for nav menus.

    Unfortunately there is a bit of repetition in the code, but when you think about what a Select does it makes sense. When you click on an option it copies that text to the selected area, i.e., clicking 1 of 4 options will not change the 4 options, but the top will now repeat the one you clicked. To do this with images would require JavaScript, orrrr... you duplicate the entries.

    In my example we have a list of games (Products), which have versions. Each product may also have Expansions, which can also have versions. For each Product we give the user a list of each version if there's more than one, along with an image and version specific text.

    <h4>@Model.Name</h4>
    @if (Model.Versions.Count == 1)
    {
        <div class="rich-select-option-body pl-2">
            <img src="@Model.Versions[0].ImageUrl" alt="">@Model.Versions[0].VersionName (@Model.Versions[0].Year)
        </div>
    }
    else
    {
        <h5>Select the version</h5>
        <div class="rich-select custom-select">
            <div class="rich-select-dropdown">
                @foreach (var version in Model.Versions)
                {
                    <div class="rich-select-option">
                        <input type="radio" name="game" id="game-@version.ProductId-@version.VersionId" @if (version == Model.Versions.First()) { @Html.Raw(" checked") ; } />
                        <div class="rich-select-option-body">
                            <label tabindex="-1">
                                <img src="@version.ImageUrl" alt="">@version.VersionName (@version.Year)
                            </label>
                        </div>
                    </div>
                }
            </div>
            <input type="checkbox" id="rich-select-dropdown-button" class="rich-select-dropdown-button" />
            <label for="rich-select-dropdown-button"></label>
            <div class="rich-select-options">
                @foreach (var version in Model.Versions)
                {
                    <div class="rich-select-option">
                        <div class="rich-select-option-body">
                            <label for="game-@version.ProductId-@version.VersionId" tabindex="-1" onclick="document.getElementById('rich-select-dropdown-button').click();">
                                <img src="@version.ImageUrl" alt=""> @version.VersionName (@version.Year)
                            </label>
                        </div>
                    </div>
                }
            </div>
        </div>
    }
    

    Using JS for the checkbox deselection we can have multiple instances on a form. Here I've extended to show a list of Expansions, which also have the same logic around versions.

    <h5 class="mt-3">Include Expansions?</h5>
    @foreach (var expansion in Model.Expansions)
    {
        <div class="form-row">
            <div class="custom-control custom-checkbox w-100">
                <input type="checkbox" class="expansion-checkbox custom-control-input" id="exp-@expansion.ProductId">
                <label class="custom-control-label w-100" for="exp-@expansion.ProductId">
    
                    @if (expansion.Versions.Count == 1)
                    {
                        <div class="rich-select-option-body pl-2">
                            <img src="@expansion.ImageUrl" />@expansion.Name: @expansion.Versions[0].VersionName (@expansion.Versions[0].Year)
                        </div>
                    }
                    else
                    {
                        <div class="rich-select custom-select">
                            <div class="rich-select-dropdown">
                                @foreach (var version in expansion.Versions)
                                {
                                    <div class="rich-select-option">
                                        <input type="radio" name="exp-@version.ProductId" id="exp-@version.ProductId-@version.VersionId" @if (version == expansion.Versions.First()) { @Html.Raw(" checked") ; } />
                                        <div class="rich-select-option-body">
                                            <label tabindex="-1">
                                                <img src="@version.ImageUrl" alt="">@expansion.Name: @version.VersionName (@version.Year)
                                            </label>
                                        </div>
                                    </div>
                                }
                            </div>
    
                            <input type="checkbox" id="rich-select-dropdown-button-@expansion.ProductId" class="rich-select-dropdown-button" />
                            <label for="rich-select-dropdown-button-@expansion.ProductId"></label>
                            <div class="rich-select-options">
                                @foreach (var version in expansion.Versions)
                                {
                                    <div class="rich-select-option">
                                        <div class="rich-select-option-body">
                                            <label for="exp-@version.ProductId-@version.VersionId" tabindex="-1" onclick="document.getElementById('rich-select-dropdown-button-@expansion.ProductId').click();">
                                                <img src="@version.ImageUrl" alt="">@expansion.Name: @version.VersionName (@version.Year)
                                            </label>
                                        </div>
                                    </div>
                                }
                            </div>
                        </div>
                    }
                </label>
            </div>
        </div>
    

    Of course this requires a fair bit of CSS, which I've only included in this JSFiddle to reduce the size of this already massive answer. I've used Bootstrap 4 to reduce the amount needed, and also to allow it to fit in with other Bootstrap controls and any site customisations that have been made.

    The images are set to 75px, but this can easily be changed in 5 lines in .rich-select and .rich-select-option-body img

    0 讨论(0)
  • 2020-11-21 12:09

    not exactly an image, but i found the easiest solution was to just add some unicode code in, ↓ works great for me

    0 讨论(0)
  • 2020-11-21 12:10

    You already have several answers that suggest using JavaScript/jQuery. I am going to add an alternative that only uses HTML and CSS without any JS.

    The basic idea is to use a set of radio buttons and labels (that will activate/deactivate the radio buttons), and with CSS control that only the label associated to the selected radio button will be displayed. If you want to allow selecting multiple values, you could achieve it by using checkboxes instead of radio buttons.

    Here is an example. The code may be a bit messier (specially compared to the other solutions):

    .select-sim {
      width:200px;
      height:22px;
      line-height:22px;
      vertical-align:middle;
      position:relative;
      background:white;
      border:1px solid #ccc;
      overflow:hidden;
    }
    
    .select-sim::after {
      content:"▼";
      font-size:0.5em;
      font-family:arial;
      position:absolute;
      top:50%;
      right:5px;
      transform:translate(0, -50%);
    }
    
    .select-sim:hover::after {
      content:"";
    }
    
    .select-sim:hover {
      overflow:visible;
    }
    
    .select-sim:hover .options .option label {
      display:inline-block;
    }
    
    .select-sim:hover .options {
      background:white;
      border:1px solid #ccc;
      position:absolute;
      top:-1px;
      left:-1px;
      width:100%;
      height:88px;
      overflow-y:scroll;
    }
    
    .select-sim .options .option {
      overflow:hidden;
    }
    
    .select-sim:hover .options .option {
      height:22px;
      overflow:hidden;
    }
    
    .select-sim .options .option img {
      vertical-align:middle;
    }
    
    .select-sim .options .option label {
      display:none;
    }
    
    .select-sim .options .option input {
      width:0;
      height:0;
      overflow:hidden;
      margin:0;
      padding:0;
      float:left;
      display:inline-block;
      /* fix specific for Firefox */
      position: absolute;
      left: -10000px;
    }
    
    .select-sim .options .option input:checked + label {
      display:block;
      width:100%;
    }
    
    .select-sim:hover .options .option input + label {
      display:block;
    }
    
    .select-sim:hover .options .option input:checked + label {
      background:#fffff0;
    }
    <div class="select-sim" id="select-color">
      <div class="options">
        <div class="option">
          <input type="radio" name="color" value="" id="color-" checked />
          <label for="color-">
            <img src="http://placehold.it/22/ffffff/ffffff" alt="" /> Select an option
          </label>
        </div>
        <div class="option">
          <input type="radio" name="color" value="red" id="color-red" />
          <label for="color-red">
            <img src="http://placehold.it/22/ff0000/ffffff" alt="" /> Red
          </label>
        </div>
        <div class="option">
          <input type="radio" name="color" value="green" id="color-green" />
          <label for="color-green">
            <img src="http://placehold.it/22/00ff00/ffffff" alt="" /> Green
          </label>
        </div>
        <div class="option">
          <input type="radio" name="color" value="blue" id="color-blue" />
          <label for="color-blue">
            <img src="http://placehold.it/22/0000ff/ffffff" alt="" /> Blue
          </label>
        </div>
        <div class="option">
          <input type="radio" name="color" value="yellow" id="color-yellow" />
          <label for="color-yellow">
            <img src="http://placehold.it/22/ffff00/ffffff" alt="" /> Yellow
          </label>
        </div>
        <div class="option">
          <input type="radio" name="color" value="pink" id="color-pink" />
          <label for="color-pink">
            <img src="http://placehold.it/22/ff00ff/ffffff" alt="" /> Pink
          </label>
        </div>
        <div class="option">
          <input type="radio" name="color" value="turquoise" id="color-turquoise" />
          <label for="color-turquoise">
            <img src="http://placehold.it/22/00ffff/ffffff" alt="" /> Turquoise
          </label>
        </div>
      </div>
    </div>

    0 讨论(0)
  • 2020-11-21 12:11

    My solution is to use FontAwesome and then add library images as text! You just need the Unicodes and they are found here: FontAwesome Reference File forUnicodes

    Here is an example state filter:

    <select name='state' style='height: 45px; font-family:Arial, FontAwesome;'>
    <option value=''>&#xf039; &nbsp; All States</option>
    <option value='enabled' style='color:green;'>&#xf00c; &nbsp; Enabled</option>
    <option value='paused' style='color:orange;'>&#xf04c; &nbsp; Paused</option>
    <option value='archived' style='color:red;'>&#xf023; &nbsp; Archived</option>
    </select>
    

    Note the font-family:Arial, FontAwesome; is required to be assigned in style for select like given in the example!

    0 讨论(0)
  • 提交回复
    热议问题