使用 jquery 在组合框上选择值

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

Select value on combobox with jquery

jqueryhtmlcombobox

提问by tinti

I have a combobox (select) and i want to select after a specified value something like that

我有一个组合框(选择),我想在指定的值之后选择类似的东西

$("mySelect").select(myValue);

Thanks

谢谢

回答by Frédéric Hamidi

If you want to select the <option>element associated with your value, you can use val():

如果要选择<option>与您的值关联的元素,可以使用val()

<select id="mySelect">
    <option value="foo">Foo</option>
    <option value="bar">Bar</option>
    <option value="quux">Quux</option>
</select>


$("#mySelect").val("quux");

回答by genesis

$("#yourselect").next().attr('selected', 'selected');

it will select next select after this one (yourselect)

它将在此之后选择下一个选择(您的选择)

so

所以

<select id="your" />aa
<select id="yourselect" />bb
<select id="yourselect2" />cc

it'll select yourselect2

它会选择你的select2

回答by Hero

$('#select').val('3');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="select">
    <option value="1">ABC</option>
    <option value="2">DEF</option>
    <option value="3">XYZ</option>
</select>