使用 jQuery 设置下拉列表的选定索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1314245/
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
Set the selected index of a Dropdown using jQuery
提问by oz.
How do I set the index of a dropdown in jQuery if the way I'm finding the control is as follows:
如果我找到控件的方式如下,如何在 jQuery 中设置下拉列表的索引:
$("*[id$='" + originalId + "']")
I do it this way because I'm creating controls dynamically and since the ids are changed when using Web Forms, I found this as a work around to find me some controls. But once I have the jQuery object, I do not know how to set the selected index to 0 (zero).
我这样做是因为我正在动态创建控件,并且在使用 Web 窗体时更改了 id,我发现这是一种解决方法,可以为我找到一些控件。但是一旦我有了 jQuery 对象,我就不知道如何将选定的索引设置为 0(零)。
回答by gnarf
First of all - that selector is pretty slow. It will scan every DOM element looking for the ids. It will be less of a performance hit if you can assign a class to the element.
首先 - 该选择器非常慢。它将扫描每个 DOM 元素以查找 id。如果您可以为元素分配一个类,那么对性能的影响就会小一些。
$(".myselect")
To answer your question though, there are a few ways to change the select elements value in jQuery
不过,要回答您的问题,有几种方法可以更改 jQuery 中的选择元素值
// sets selected index of a select box to the option with the value "0"
$("select#elem").val('0');
// sets selected index of a select box to the option with the value ""
$("select#elem").val('');
// sets selected index to first item using the DOM
$("select#elem")[0].selectedIndex = 0;
// sets selected index to first item using jQuery (can work on multiple elements)
$("select#elem").prop('selectedIndex', 0);
回答by 4imble
Just found this, it works for me and I personally find it easier to read.
刚刚找到这个,它对我有用,我个人觉得它更容易阅读。
This will set the actual index just like gnarf's answer number 3 option.
这将设置实际索引,就像 gnarf 的答案编号 3 选项一样。
// sets selected index of a select box the actual index of 0
$("select#elem").attr('selectedIndex', 0);
This didn't used to work but does now... see bug: http://dev.jquery.com/ticket/1474
这过去没有用,但现在可以了……见错误:http: //dev.jquery.com/ticket/1474
Addendum
附录
As recommended in the comments use :
正如评论中所推荐的那样:
$("select#elem").prop('selectedIndex', 0);
$("select#elem").prop('selectedIndex', 0);
回答by PaulG
I'm writing this answer in 2015, and for some reason (probably older versions of jQuery) none of the other answers have worked for me. I mean, they change the selected index, but it doesn't actually reflect on the actual dropdown.
我在 2015 年写了这个答案,出于某种原因(可能是旧版本的 jQuery),其他答案都没有对我有用。我的意思是,他们更改了选定的索引,但实际上并没有反映在实际的下拉列表中。
Here is another way to change the index, and actually have it reflect in the dropdown:
这是更改索引的另一种方法,实际上让它反映在下拉列表中:
$('#mydropdown').val('first').change();
回答by djs
I'm using
我正在使用
$('#elem').val('xyz');
to select the option element that has value='xyz'
选择具有 value='xyz' 的选项元素
回答by sathya
JQuery code:
查询代码:
$("#sel_status").prop('selectedIndex',1);
Jsp Code:
JS代码:
Status:
<select name="sel_status"
id="sel_status">
<option value="1">-Status-</option>
<option>ALL</option>
<option>SENT</option>
<option>RECEIVED</option>
<option>DEACTIVE</option>
</select>
回答by Jarrod
To select the 2nd option
选择第二个选项
$('#your-select-box-id :nth-child(2)').prop('selected', true);
Here we add the `trigger('change') to make the event fire.
这里我们添加了`trigger('change') 来触发事件。
$('#your-select-box-id :nth-child(2)').prop('selected', true).trigger('change');
回答by MacAnthony
You want to grab the value of the first option in the select element.
您想获取 select 元素中第一个选项的值。
$("*[id$='" + originalId + "']").val($("*[id$='" + originalId + "'] option:first").attr('value'));
回答by Josh Mein
$("[id$='" + originalId + "']").val("0 index value");
will set it to 0
将其设置为 0
回答by dabar
Select 4th option
选择第四个选项
$('#select').val($('#select option').eq(3).val());