jQuery 如何获取 select->option 标签的索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1817079/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 12:11:40  来源:igfitidea点击:

How to get the Index of select->option tag

jqueryindexing

提问by Vov4ik

<select id="sel">
<option value="123" selected="selected">text1</option>
<option value="44">text2</option>
<option value="882">text3</option>
...
</select>

How to get the index of selected option with jQuery? May be .index(subject), but all possibilities tested, didn't work...

如何使用jQuery获取所选选项的索引?可能是.index(subject),但所有的可能性都经过测试,没有奏效...

P.S. Indexes: value="123" => 0, value="44" => 1, ...

PS 索引:value="123" => 0,value="44" => 1,...

Thanx

谢谢

采纳答案by Bob

This will do it:

这将做到:

   $("#sel").attr("selectedIndex")

回答by Chris Moschini

Only Bob's second answer is correct:

只有鲍勃的第二个答案是正确的:

$("#sel")[0].selectedIndex

Works: http://jsfiddle.net/b9chris/wxeVN/1/

作品:http: //jsfiddle.net/b9chris/wxeVN/1/

Using .attr()works only if the user (or browser's DOM restore) has not changed the option selected since the page loaded: http://jsfiddle.net/b9chris/wxeVN/

.attr()仅当用户(或浏览器的 DOM 恢复)未更改自页面加载后选择的选项时才使用:http: //jsfiddle.net/b9chris/wxeVN/

You could implement this as a jQuery extension, and get a little more info in the process:

您可以将其实现为 jQuery 扩展,并在此过程中获得更多信息:

(function($) {
    $.fn.selectedOption = function() {
        var sel = this[0];
        return sel.options[sel.selectedIndex];
    };
})(jQuery)

$('button').click(function() {
    $('#output').text('selected index: ' + $('select').selectedOption().index);
});

http://jsfiddle.net/b9chris/wxeVN/102/

http://jsfiddle.net/b9chris/wxeVN/102/

What's returned by .selectedOption()is the actual option tag, so you can access .index, .value, and .text- a bit more convenient than just the index in typical usage.

什么是通过返回.selectedOption()的是实际的选项标签,这样你就可以访问.index.value以及.text-有点不仅仅是在典型使用该指数更方便。

回答by Justin Morgan

As I write this, two of the top answers (including the accepted answer) are incorrect despite being pointed out nearly five years ago. attr("selectedIndex")does nothing, because selectedIndexis a property on the actual DOM element, not an HTML attribute. You need to use prop:

在我写这篇文章时,尽管在近五年前被指出,但其中两个最重要的答案(包括已接受的答案)是不正确的。attr("selectedIndex")什么都不做,因为它selectedIndex是实际 DOM 元素上的属性,而不是 HTML 属性。您需要使用prop

$(this).prop('selectedIndex')

Interactive demo comparing this to the incorrect version: http://jsfiddle.net/uvwkD/

将此与错误版本进行比较的交互式演示:http: //jsfiddle.net/uvwkD/

回答by Bob

$("#sel").attr("selectedIndex")

or

或者

$("#sel")[0] //to get the DOM element
$("#sel")[0].selectedIndex

回答by antti_s

You can get the index of the element in this case by checking how many sibling elements the selected element has before it:

在这种情况下,您可以通过检查所选元素之前有多少个同级元素来获取元素的索引:

$('#sel option:selected').prevAll().length;

回答by Jacob Relkin

You can actually do it without jQuery: var sel = document.getElementById( 'sel' ); var index = sel.selectedIndex;

你实际上可以在没有 jQuery 的情况下做到这一点: var sel = document.getElementById( 'sel' ); var index = sel.selectedIndex;

回答by user167517

Use the standard index function of jquery like in this code example

在这个代码示例中使用 jquery 的标准索引函数

$("#sel option:selected").index()

$("#sel option:selected").index()

回答by user1566694

The title doesn't quite match the question...

标题和问题不太相符...

How to get the Index of select->option tag

如何获取 select->option 标签的索引

option.index // javascript

$(option).prop('index') // jquery


index of selectedselect->option tag:

所选select->option 标签的索引:

document.getElementById('sel').selectedIndex // javascript