从下拉列表中获取未使用 jquery 选择的值列表

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

Get list of values from dropdown that are NOT selected using jquery

jquerydrop-down-menu

提问by Amanda Kitson

I have two related dropdowns.

我有两个相关的下拉列表。

When the user selects an option from the first drop down, the second drop down is populated with a list of all the options from the first drop down that were NOT selected.

当用户从第一个下拉列表中选择一个选项时,第二个下拉列表将填充第一个下拉列表中未选择的所有选项的列表。

I'm trying to use jquery to get all the not selected options, but I'm still a jquery newb and must be mising something.

我正在尝试使用 jquery 来获取所有未选中的选项,但我仍然是 jquery 新手,一定是缺少某些东西。

I am trying the following:

我正在尝试以下操作:

$('#segmentCrossStreet1:not(:selected)')

This is where "segmentCrossStreet1" is the ID of the first drop down. This doesn't appear to return anything useful. What am I doing wrong?

这就是“segmentCrossStreet1”是第一个下拉列表的 ID。这似乎没有返回任何有用的东西。我究竟做错了什么?

回答by Rob W

Fiddle: http://jsfiddle.net/uzhWS/(this fiddle also shows how to pupulate another <select>)

小提琴:http: //jsfiddle.net/uzhWS/(这个小提琴也展示了如何化身另一个<select>

You have to select the <option>elements, rather than the "selected <select>" elements:

您必须选择<option>元素,而不是“选定的<select>”元素:

$('#segmentCrossStreet1 option:not(:selected)');

回答by Blender

Your current selector:

您当前的选择器:

$('#segmentCrossStreet1:not(:selected)')

Searches for all #segmentCrossStreet1elements that are not selected. Is this what you want? I doubt it. Add a space before :not(to search for childelements:

搜索所有#segmentCrossStreet1未选择的元素。这是你想要的吗?我对此表示怀疑。在:not(搜索元素之前添加一个空格:

$('#segmentCrossStreet1: not(:selected)')

Better if you made it more specific:

如果你让它更具体,那就更好了:

$('#segmentCrossStreet1: option:not(:selected)')