jQuery从中选择选项 [英] jquery selecting option from
本文介绍了jQuery从中选择选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下选项框:
<label for="inputselection1">Input #1:</label>
<select id="inputselection1" name="unittype">
<option value="1">Miner</option>
<option value="2">Puffer</option>
<option value="3">Snipey</option>
<option value="4">Max</option>
<option value="5">Firebot</option>
</select><br>
但是,当我执行以下jquery选项onload时,文本(选定的选项)不会更改为Max:
however, when I execute the following jquery option onload, the text (selected option) does not get changed to Max:
$("#inputselection1 option[text=Max]").attr("selected","selected");
有什么作用?
谢谢!
推荐答案
使用.val()
通过选项值设置选择值:
Use .val()
to set select value by option value:
$("#inputselection1").val(4);
如果您坚持按文本进行选择,请使用以下方法:
If you insist on selecting by text, then use this:
$("#inputselection1 option").filter(function() {
return $(this).text() === 'Max';
}).attr("selected", "selected");
这篇关于jQuery从中选择选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文