我有一个下拉列表:
<select id="HowYouKnow" >
<option value="1">FRIEND</option>
<option value="2">GOOGLE</option>
<option value="3">AGENT</option></select>
在上面的下拉列表中,我知道下拉列表的文本。如何使用jquery通过文本在document.ready中设置下拉列表的值?
发布于 2013-04-20 08:35:44
要获得精确匹配,请使用
$("#HowYouKnow option").filter(function(index) { return $(this).text() === "GOOGLE"; }).attr('selected', 'selected');
contains将选择可能不精确的最后一个匹配项。
发布于 2010-12-01 15:31:27
$("#HowYouKnow option[value='" + theText + "']").attr('selected', 'selected'); // added single quotes
发布于 2012-02-09 00:29:54
var myText = 'GOOGLE';
$('#HowYouKnow option').map(function() {
if ($(this).text() == myText) return this;
}).attr('selected', 'selected');
https://stackoverflow.com/questions/1888931
复制相似问题