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

时间:2022-11-26 23:02:07
<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(主题),但所有测试的可能性都没有用......

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

附:索引:value =“123”=> 0,value =“44”=> 1,...

Thanx

感谢名单

8 个解决方案

#1


3  

This will do it:

这样做:

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

#2


17  

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 - 比典型用法中的索引更方便一些。

#3


10  

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 selectedIndex is a property on the actual DOM element, not an HTML attribute. You need to use prop:

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

$(this).prop('selectedIndex')

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

互动演示将此与不正确的版本进行比较:http://jsfiddle.net/uvwkD/

#4


5  

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

or

要么

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

#5


2  

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;

#6


2  

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;

#7


1  

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

使用jquery的标准索引函数,就像在这个代码示例中一样

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

$(“#sel option:selected”)。index()

#8


0  

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 selected select->option tag:

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

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

#1


3  

This will do it:

这样做:

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

#2


17  

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 - 比典型用法中的索引更方便一些。

#3


10  

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 selectedIndex is a property on the actual DOM element, not an HTML attribute. You need to use prop:

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

$(this).prop('selectedIndex')

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

互动演示将此与不正确的版本进行比较:http://jsfiddle.net/uvwkD/

#4


5  

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

or

要么

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

#5


2  

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;

#6


2  

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;

#7


1  

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

使用jquery的标准索引函数,就像在这个代码示例中一样

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

$(“#sel option:selected”)。index()

#8


0  

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 selected select->option tag:

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

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