在 jQuery 中获取选定的选项值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2662738/
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 13:58:57  来源:igfitidea点击:

Getting the selected option value in jQuery

jquery

提问by Misha Moroshko

I would like to print the selected values of all selects using jQuery. I did it like this, but I feel that there is a nicer way to write the same. Am I right ?

我想使用 jQuery 打印所有选择的选定值。我是这样做的,但我觉得有一种更好的方式来写同样的东西。我对吗 ?

$("select").each(function() {
    alert(this.options[this.selectedIndex].value);
});

回答by SLaks

Yes.

是的。

alert($(this).val());

jQuery's valfunctionwill return the value of the selected option from a <select>element.

jQuery 的val函数将从<select>元素返回所选选项的值。