jquery - 如何在<option>标签内插入<img>标签?

标签 jquery html css jsp

我想显示从 Controller 传递到 JSP 的数据的图像文件。

例如,当您从 JSP 发送以下值时,

{selectDoctor=[{d_name=MyName, d_imageServerPath=/boardFile/014e40e0ea.PNG, d_imagePath=C:/rachel/014e40e0ea.PNG, di_idx=2, d_imageOriginalName=testImage.PNG, d_idx=2, d_medicalInformation=valuevalue}]}

我已经能够通过语句显示标签外的值了。

<c:forEach items="${selectDoctor}" var="sD">
    <select>
        <option value="">Choice Doctor</option>
        <option value="${sD.d_idx}"><img src="${sD.di_imageServerPath}></img>${sD.d_name}</option>
    </select>
</c:forEach>

但与我的预期不同,img 标签不起作用。

如何在标签内插入标签?

最佳答案

来源链接http://jqueryui.com/selectmenu/#custom_render

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>jQuery UI Selectmenu - Custom Rendering</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  <link rel="stylesheet" href="/resources/demos/style.css">
  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
  <script>
  $( function() {
    $.widget( "custom.iconselectmenu", $.ui.selectmenu, {
      _renderItem: function( ul, item ) {
        var li = $( "<li>" ),
          wrapper = $( "<div>", { text: item.label } );
 
        if ( item.disabled ) {
          li.addClass( "ui-state-disabled" );
        }
 
        $( "<span>", {
          style: item.element.attr( "data-style" ),
          "class": "ui-icon " + item.element.attr( "data-class" )
        })
          .appendTo( wrapper );
 
        return li.append( wrapper ).appendTo( ul );
      }
    });
 
    $( "#filesA" )
      .iconselectmenu()
      .iconselectmenu( "menuWidget" )
        .addClass( "ui-menu-icons" );
 
    $( "#filesB" )
      .iconselectmenu()
      .iconselectmenu( "menuWidget" )
        .addClass( "ui-menu-icons customicons" );
 
    $( "#people" )
      .iconselectmenu()
      .iconselectmenu( "menuWidget")
        .addClass( "ui-menu-icons avatar" );
  } );
  </script>
  <style>
    h2 {
      margin: 30px 0 0 0;
    }
    fieldset {
      border: 0;
    }
    label {
      display: block;
    }
 
    /* select with custom icons */
    .ui-selectmenu-menu .ui-menu.customicons .ui-menu-item-wrapper {
      padding: 0.5em 0 0.5em 3em;
    }
    .ui-selectmenu-menu .ui-menu.customicons .ui-menu-item .ui-icon {
      height: 24px;
      width: 24px;
      top: 0.1em;
    }
    .ui-icon.video {
      background: url("images/24-video-square.png") 0 0 no-repeat;
    }
    .ui-icon.podcast {
      background: url("images/24-podcast-square.png") 0 0 no-repeat;
    }
    .ui-icon.rss {
      background: url("images/24-rss-square.png") 0 0 no-repeat;
    }
 
    /* select with CSS avatar icons */
    option.avatar {
      background-repeat: no-repeat !important;
      padding-left: 20px;
    }
    .avatar .ui-icon {
      background-position: left top;
    }
  </style>
</head>
<body>
 
<div class="demo">
 
<form action="#">
  <h2>Selectmenu with custom avatar 16x16 images</h2>
  <fieldset>
    <label for="people">Select a Person:</label>
    <select name="people" id="people">
      <option value="1" data-class="avatar" data-style="background-image: url(&apos;http://www.gravatar.com/avatar/b3e04a46e85ad3e165d66f5d927eb609?d=monsterid&amp;r=g&amp;s=16&apos;);">John Resig</option>
      <option value="2" data-class="avatar" data-style="background-image: url(&apos;http://www.gravatar.com/avatar/e42b1e5c7cfd2be0933e696e292a4d5f?d=monsterid&amp;r=g&amp;s=16&apos;);">Tauren Mills</option>
      <option value="3" data-class="avatar" data-style="background-image: url(&apos;http://www.gravatar.com/avatar/bdeaec11dd663f26fa58ced0eb7facc8?d=monsterid&amp;r=g&amp;s=16&apos;);">Jane Doe</option>
    </select>
  </fieldset>
</form> 
</div>  
</body>
</html>

关于jquery - 如何在<option>标签内插入<img>标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47747738/

相关文章:

jquery - 从具有唯一 id 的 DIV 内的 <a> 复制 href 并将其粘贴到该 DIV 中的其他位置

javascript - 克隆时的问题 - jquery

html - 悬停元素,在父元素 sibling child 上显示悬停效果

javascript - 输入显示在我的固定标题中

python - Django:如何从 .css 文件链接到静态文件目录?

javascript - 使用 Prism JS 不显示标记

javascript - Jquery 方法(单击、更改)不起作用

PHP - 脚本未被执行

jquery - 使用 ajax、jquery 动态附加时,owl 轮播项目不起作用

javascript - 使用 jquery 根据操作系统隐藏 CSS 元素