简体   繁体   English

jQuery和访问下拉值

[英]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 the same. 获取输入框的值没有问题,但是我对如何获取下拉框中选择的内容感到有些困惑-似乎无法正常工作。 At the moment everything defaults to the first if statement of converting from FPS to MPH even if that isn't selected. 目前,即使未选择从FPS转换为MPH的所有if语句,所有内容都默认为第一个if语句。

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. 目前,我只是“实现”了从FPS转换为MPH / KMH / MS的数学方法,看来我将要获得很多if else语句,因此也想知道是否有更好的方法关于它。

If your left confused I'd suggest just looking at it: http://jsfiddle.net/Deva/RPkpW/ 如果您的左手感到困惑,我建议您看一下: http : //jsfiddle.net/Deva/RPkpW/

Use jQuery's .val() method to obtain the value of a dropdown. 使用jQuery的.val()方法获取下拉列表的值。

For example, to obtain the value of the selected <option> in <select name="types"> , 例如,要获取<select name="types">所选<option>的值,

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. 您的标记省略了<option>元素上的value属性,因此(按照规范)这些元素的值默认为标记中的文本。

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) 它的工作原理与从输入框中获取它的工作原理相同,您可以分配选择一个ID,就像使用$(“#idhere”)在输入框中输入ID一样,然后将所选下拉列表的值拉下来,不同之处在于您分配了选项的值(请参见下文)

     <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. 使用$(“#example”)定位时返回的值将等于当时选择的任何下拉列表的值。

Here is some working code to show you how to work with it 这是一些工作代码向您展示如何使用它

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

Change, get and determine what to do based on the values of the comboboxes. 根据组合框的值进行更改,获取并确定要执行的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM