简体   繁体   English

下拉列表(选择框)获取选择名称

[英]dropdown (select box) get Name of selection

I have this code on Fiddle Code 我在小提琴代码上有这个代码

I have the picture portion of my code working, where it will change picture every time you select a value. 我的代码的图片部分工作,每次选择一个值时它都会改变图片。

What I want to display is the actual Name " Image 1 " of the value.. so Image 1 etc. 我要显示的是值的实际名称“图像1”..所以图像1等

I went through stackoverflow and found some examples, and I tried to implement them but i can't get it to work. 我经历了stackoverflow并找到了一些例子,我试图实现它们,但我无法让它工作。

So in short, when you select an a value... it will show picture and in the empty div it will show the name 因此,简而言之,当您选择一个值...它将显示图片,而在空div中它将显示名称

$(document).ready(function() {

   $("#image").change(function() {
     $("#imagePreview").empty();
        $("#imagePreview").append("<img src=\"" + $("#image").val()  + "\" />");
   }).change();
 });

$('#image option:selected').text(); {

   $( "div" ).text( str );
}

<select name="image" id="image" class="inputbox" size="1">
   <option value="imageall.jpg" selected> - All - </option>
   <option value="image1.jpg">image1.jpg</option>
   <option value="image2.jpg">image2.jpg</option>
   <option value="image3.jpg">image3.jpg</option>
</select>

<div id="imagePreview">
   displays image here
</div>

<div></div>

EDIT: Something like this : EXAMPLE 编辑:这样的事情: 例子

Try, 尝试,

$(document).ready(function () {
    $("#image").change(function () {
        $("#imagePreview").empty();
        $("#imagePreview").append("<img src=\"" + $(this).val() + "\" />");
        $("div:last").text($('option:selected', this).text());
    }).change();
});

DEMO DEMO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM