开发者

jQuery and accessing drop down values

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 t开发者_StackOverflow中文版he 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/


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜