从数组 Jquery 中获取下拉列表的选定索引

Get selected index of dropdown from array Jquery

本文关键字:索引 下拉列表 获取 数组 Jquery      更新时间:2023-09-26

我正在将所有输入以数组的形式保存到变量中。

示例:var data = $('inputs,select')

现在,我想使用变量数据获取下拉列表的选定索引。

请帮忙。

编辑:添加了小提琴以供参考

小提琴

如果我正确理解了您的问题,则在每个输入中保存一个jQuery对象并在变量中进行选择。若要获取下拉列表的选定索引,必须遍历变量以确定它是选择输入还是常规输入,然后获取其所选索引。

//loop over every dom element in the variable
data.each(function () {
    //if its a select 
    if ($(this).is("select")) {
        //find its selected index using native DOM and do something with it
        $(this)[0].selectedIndex;
    }
});

您可能希望添加$( "select option:selected" ).text();以选择所选项目。

这是来自: http://learn.jquery.com/using-jquery-core/faq/how-do-i-get-the-text-value-of-a-selected-option/