1

I'm trying to perform different actions on a value placed in an input box depending on the selection made in two drop down boxes. On this occasion it is converting speeds. I have no problems obtaining the value of the input box but I'm a little stumped as to how I can obtain what has been selected in the drop down boxes - it doesn't seem to work the same. At the moment everything defaults to the first if statement of converting from FPS to MPH even if that isn't selected.

I've only 'implemented' the maths for converting from FPS to MPH/KMH/MS at the moment and it looks like I'm going to end up with a lot of if else statements so also wondering if there might be a better way about it.

If your left confused I'd suggest just looking at it: http://jsfiddle.net/Deva/RPkpW/

Matt
  • 720
  • 8
  • 15

3 Answers3

3

Use jQuery's .val() method to obtain the value of a dropdown.

For example, to obtain the value of the selected <option> in <select name="types">,

var selected = $('#fpsConvForm').find('select[name="types"]').val();

Your markup omits the value attribute on the <option> elements, so (as per the spec) the value of those elements defaults to the text in the tags.

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • that's great. i've updated the jsfiddle and i still have problems. i know it is definitely getting the right value as i've checked by printing them out. hence i assume i've got the logic wrong somewhere as it assumes the first if statement is always correct? – Matt May 18 '11 at 14:56
  • scratch that - i was using one `=` rather than two i.e. `==` i notice 3 also works. what's the rules there? – Matt May 18 '11 at 15:29
0

it should work the same as obtaining it from the input boxes, you can assign the select an ID just like you could the input box using $("#idhere") and pull the value of the selected drop down the difference is that you assign the values to the options (see below)

     <SELECT id="example">
     <OPTION VALUE=a SELECTED>example1
     <OPTION VALUE=b>example2
     <OPTION VALUE=c>example3
     <OPTION VALUE=d>example4
     </SELECT>

the value that is returned when targeting this with the $("#example") would be equivalent to the value of whatever drop down is selected at the time.

Gordnfreeman
  • 1,615
  • 1
  • 15
  • 24
0

Here is some working code to show you how to work with it

http://jsfiddle.net/ppumkin/UwddM/

Change, get and determine what to do based on the values of the comboboxes.

Piotr Kula
  • 9,597
  • 8
  • 59
  • 85