views:

210

answers:

3

Hello,

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?

+1  A: 
Pointy
Thanks pointy, it's applicable in the applications right now? I've tried to use it, but it doesn't display the picture.
garcon1986
Uhh ... I'm not sure what you mean. Maybe if you posted more of your code it would help.
Pointy
I have updated the post, can you check it?
garcon1986
OK I added to my answer. You have to use "getAttribute" to fetch those things.
Pointy
Yes, you're right! Great! Thanks!
garcon1986
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?
garcon1986
You could reinitialize the page in the handler for the "load" event on the window. `window.onload=function() { ... }`
Pointy
I can use onload to assure the picture i have chosen stays the same?
garcon1986
You can use a handler for "load" to reset the page to whatever initial state you think it should be in. If there's a "default" picture and you want the dropdown list to show the corresponding value, then your "load" handler would force the `<select>` to show that one by setting "selectedIndex" to the right value.
Pointy
i don't understand it very well. Normally there is an default image, when i click one item in the drop down list and submit, it will show the default image, but the item is still what i have chosen.
garcon1986
I suggest that you ask a new question about this - maybe somebody will understand better than I do!
Pointy
+1  A: 

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.

Sijin
A map or an associative array really is the best and foolproof way of doing this, imo.
Kyle Hayes
A: 

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>
Ed B
That's certainly possible, but it seems icky to have to burden the server-side code with throwing away the extra stuff, or to have to deal with that upon form submission. Of course, if there's no server-side action involved, then it doesn't matter.
Pointy
How to use the second parameter in the 'value' property?
garcon1986
Depends on where you are using the Value...in Javascript or C#.In JavaScript, you have to split the value into an array of substrings, then select the second element in the array that's created:http://www.tizag.com/javascriptT/javascript-string-split.phpIn C#, same idea..use the split method on strings:http://dotnetperls.com/string-splitthen select the second element in the array.
Ed B