如何使用下拉列表更改图片?

发布于 2024-09-03 08:26:08 字数 854 浏览 3 评论 0原文

我想使用一个下拉列表,每次单击其中的内容时,都会显示不同的图片。但我不想使用属性“值”,因为我想将这些值用于其他用途。

示例:

<select name='test'>
<option value='1'>1</option>
<option value='2'>2</option>
<option value='3'>3</option>
<option value='4'>4</option>
</select>

我怎样才能做到这一点?

谢谢。

编辑:

根据pointy的建议,它可以工作。

<select name="dpt" onChange="picture.src=this.options[this.selectedIndex].getAttribute('data-whichPicture');" >
<option value="1839" data-whichPicture='./imgs/t1.jpg' >01</option>
<option value="1661" data-whichPicture='./imgs/t2.png' >02</option>
</select>
</select>
<img src='' id='picture' />

编辑:

有一个问题,即如果我刷新页面,将显示默认图片,但下拉列表中的选择将保留。如何解决这个问题?

I want to use a drop down list, every time i click the content in it, different pictures will show up. But i don't want to use the property 'value', because i want to use the values for other use.

Example:

<select name='test'>
<option value='1'>1</option>
<option value='2'>2</option>
<option value='3'>3</option>
<option value='4'>4</option>
</select>

How do i accomplish that?

Thanks.

Edit:

with pointy's suggestion, it works.

<select name="dpt" onChange="picture.src=this.options[this.selectedIndex].getAttribute('data-whichPicture');" >
<option value="1839" data-whichPicture='./imgs/t1.jpg' >01</option>
<option value="1661" data-whichPicture='./imgs/t2.png' >02</option>
</select>
</select>
<img src='' id='picture' />

Edit:

There is a problem, that is if i refresh the page, the default the picture will display but the selection in the drop down list will stay. How to fix that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

若有似无的小暗淡 2024-09-10 08:26:09

您可以在“值”字段中放置多个值,并用分隔符分隔它们。
您必须拆分该值并选择显示图像时要使用的值

<option value="1,dogs.jpg">1</option>
<option value="2,hogs.jpg">2</option>
<option value="3,cats.jpg">3</option>
<option value="4,dogs.jpg">4</option>

You can put multiple values in the 'value' field by separating them with delimiters.
You would have to split the value and select the one you want to use when displaying your image

<option value="1,dogs.jpg">1</option>
<option value="2,hogs.jpg">2</option>
<option value="3,cats.jpg">3</option>
<option value="4,dogs.jpg">4</option>
星星的軌跡 2024-09-10 08:26:08

您可以使用每个选项的“类”来保存要显示的图像的一些指示符。如果您像当今所有美丽的人一样使用 HTML5 编写,则可以使用“数据”属性:

<option value='1' data-whichPicture='http://whatever.com/picture_017.jpg'>1</option>

实际上您可以在标签上粘贴任何您想要的属性,但它会使页面处于无效状态。 HTML5 属性尚未经过验证(我不认为;我上次尝试的 HTML5 验证器相当不可靠),但 W3C 承诺有一天会进行验证。

编辑 - 另外 - 您的问题提到您想要在有人“单击” 并使用向上和向下箭头键来更改选择。在用户退出

编辑 - 获取属性(如果您使用“data-”技巧) ,您必须在元素上使用“getAttribute”方法:

var whichPicture = sel.options[sel.selectedIndex].getAttribute('data-whichPicture');

You could use the "class" of each option to hold some indicator of what image to show. If you're writing in HTML5 like all of the Beautiful People nowadays, you can use a "data" attribute:

<option value='1' data-whichPicture='http://whatever.com/picture_017.jpg'>1</option>

You can in fact stick any attribute you want on your tags, but it'll leave the page in a non-valid state. The HTML5 attributes aren't validated yet (I don't think; the HTML5 validator I last tried was pretty dodgy), but the W3C promises that they will be someday.

edit — also - your question mentions that you want to change pictures when somebody "clicks" on the <select>. You can in fact get "click" events from each <option>, but you should probably also watch for "change" events. Users can tab into your <select> and use up- and down-arrow keys to change selection. You won't get the "change" event until the user tabs out of the <select> or clicks somewhere else. (It might be possible to get "keypress" events; I haven't tried that but I just might ...)

edit — To get the attributes (if you use the "data-" trick), you have to use the "getAttribute" method on the elements:

var whichPicture = sel.options[sel.selectedIndex].getAttribute('data-whichPicture');
π浅易 2024-09-10 08:26:08

您可以在选项值和图像之间设置映射

{ 1: "imgA.png", 2: "imgB.png" }

,然后使用函数根据此映射加载正确的图像。

另一种方法可能是使用图像名称来增加值,如下所示,

<select name='test'>
<option value='1;imgA.png'>1</option>
<option value='2;imgB.png'>2</option>
</select>

然后使用字符串函数根据需要解析出数值或图像名称。

You can set up a map between the option values and the images

{ 1: "imgA.png", 2: "imgB.png" }

and then use a function to load the correct image based on this mapping.

Another approach might be to augment the value with the image name, like this,

<select name='test'>
<option value='1;imgA.png'>1</option>
<option value='2;imgB.png'>2</option>
</select>

And then use string functions to parse out the number value or the image name as required.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文