如何获取下拉列表的选定索引 [英] how to get the selected index of a drop down
本文介绍了如何获取下拉列表的选定索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个正常的下拉列表,我希望得到当前选择的索引并将其放入变量中。 Jquery或javascript。 Jquery perfered。
I have a normal dropdown which I want to get the currently selected index and put that in a variable. Jquery or javascript. Jquery perfered.
<select name="CCards">
<option value="0">Select Saved Payment Method:</option>
<option value="1846">test xxxx1234</option>
<option value="1962">test2 xxxx3456</option>
</select>
推荐答案
$(select [ name ='CCards']选项:选中)
应该做的诀窍
有关更多详细信息,请参阅jQuery文档: http://api.jquery.com/selected-selector/
See jQuery documentation for more detail: http://api.jquery.com/selected-selector/
更新:
如果您需要所选选项的索引,则需要使用 .index()
jquery方法:
UPDATE:
if you need the index of the selected option, you need to use the .index()
jquery method:
$("select[name='CCards'] option:selected").index()
这篇关于如何获取下拉列表的选定索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文