如何使用下拉列表更改图片?
我想使用一个下拉列表,每次单击其中的内容时,都会显示不同的图片。但我不想使用属性“值”,因为我想将这些值用于其他用途。
示例:
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以在“值”字段中放置多个值,并用分隔符分隔它们。
您必须拆分该值并选择显示图像时要使用的值
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
您可以使用每个选项的“类”来保存要显示的图像的一些指示符。如果您像当今所有美丽的人一样使用 HTML5 编写,则可以使用“数据”属性:
实际上您可以在标签上粘贴任何您想要的属性,但它会使页面处于无效状态。 HTML5 属性尚未经过验证(我不认为;我上次尝试的 HTML5 验证器相当不可靠),但 W3C 承诺有一天会进行验证。
编辑 - 另外 - 您的问题提到您想要在有人“单击”
并使用向上和向下箭头键来更改选择。在用户退出
编辑 - 获取属性(如果您使用“data-”技巧) ,您必须在元素上使用“getAttribute”方法:
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:
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:
您可以在选项值和图像之间设置映射
,然后使用函数根据此映射加载正确的图像。
另一种方法可能是使用图像名称来增加值,如下所示,
然后使用字符串函数根据需要解析出数值或图像名称。
You can set up a map between the option values and the images
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,
And then use string functions to parse out the number value or the image name as required.