jQuery 使用jQuery获取在下拉列表中选择的当前值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4874124/
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
Get current value selected in dropdown using jQuery
提问by Amit
I have a set of dynamically generated dropdown boxes on my page. basically I clone them using jQuery. now I want to capture the value selected on each dropdown on change event.
我的页面上有一组动态生成的下拉框。基本上我使用 jQuery 克隆它们。现在我想捕获在更改事件的每个下拉列表中选择的值。
I tried something like this which did not work.
我试过这样的东西,但没有用。
$('._someDropDown').live('change', function(e) {
//debugger;
var v = $(this);
alert($(this + ':selected').val());
alert($(this).val());
});
How do I get it done?
我该如何完成?
回答by Robin Rizvi
To get the text of the selected option
获取所选选项的文本
$("#your_select :selected").text();
To get the value of the selected option
获取所选选项的值
$("#your_select").val();
回答by Olical
This is what you need :)
这就是你需要的:)
$('._someDropDown').live('change', function(e) {
console.log(e.target.options[e.target.selectedIndex].text);
});
For new jQuery use on
对于新的 jQuery 使用 on
$(document).on('change', '._someDropDown', function(e) {
console.log(this.options[e.target.selectedIndex].text);
});
回答by Dharmesh
$("#citiesList").change(function() {
alert($("#citiesList option:selected").text());
alert($("#citiesList option:selected").val());
});
citiesList is id of select tag
cityList 是选择标签的 id
回答by Sukhjeevan
Check it Out-->
检查出来-->
For getting text
用于获取文本
$("#selme").change(function(){
$(this[this.selectedIndex]).text();
});
For getting value
为了获得价值
$("#selme").change(function(){
$(this[this.selectedIndex]).val();
});
回答by Xhalent
To get the value of a drop-down (select) element, just use val().
要获取下拉(选择)元素的值,只需使用 val()。
$('._someDropDown').live('change', function(e) {
alert($(this).val());
});
If you want to the text of the selected option, using this:
如果要选择选项的文本,请使用:
$('._someDropDown').live('change', function(e) {
alert($('[value=' + $(this).val() + ']', this).text());
});
回答by alexl
You can try:
你可以试试:
$("._someDropDown").val();
回答by Mazzy
This is actually more efficient and has better readability in my opinion if you want to access your select with thisor another variable
如果您想使用这个或另一个变量访问您的选择,这实际上更有效并且在我看来具有更好的可读性
$('#select').find('option:selected')
In fact if I remember correctly phpStorm will attempt to auto correct the other method.
事实上,如果我没记错的话,phpStorm 会尝试自动更正另一种方法。
回答by Olivier.h
In case you want the index of the current selected value.
如果您想要当前选定值的索引。
$selIndex = $("select#myselectid").prop('selectedIndex'));
回答by Vivek
try this...
尝试这个...
$("#yourdropdownid option:selected").val();
回答by vijay pujar
The options discussed above won't work because they are not part of the CSS
specification (it is jQuery
extension). Having spent 2-3 days digging around for information, I found that the only way to select the Text of the selected option from the drop down is:
上面讨论的选项将不起作用,因为它们不是CSS
规范的一部分(它是 jQuery
扩展)。花了 2-3 天寻找信息后,我发现从下拉列表中选择所选选项的文本的唯一方法是:
{ $("select", id:"Some_ID").find("option[selected='selected']")}
Refer to additional notes below:
Because :selected is a jQuery
extension and not part of the CSS
specification, queries using :selected cannot take advantage of the performance boost provided by the native DOM querySelectorAll() method. To achieve the best performance when using :selected to select elements, first select the elements using a pure CSS
selector, then use .filter(":selected")
. (copied from: http://api.jquery.com/selected-selector/)
请参阅下面的附加说明: 因为 :selected 是jQuery
扩展而不是CSS
规范的一部分,所以使用 :selected 的查询无法利用原生 DOM querySelectorAll() 方法提供的性能提升。为了在使用 :selected 选择元素时获得最佳性能,首先使用纯CSS
选择器选择元素,然后使用.filter(":selected")
. (复制自:http: //api.jquery.com/selected-selector/)