使用jquery通过文本设置下拉列表值 [英] set dropdown value by text using jquery
本文介绍了使用jquery通过文本设置下拉列表值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个下拉列表:
< select id =HowYouKnow>
< option value =1> FRIEND< / option>
< option value =2> GOOGLE< / option>
< option value =3> AGENT< / option>< / select>
在上面的下拉菜单中,我知道下拉菜单的文字。如何使用jquery将文本中的下拉列表的值设置为文本?
解决方案
这是一种基于在该选项的文本上,而不是索引。刚刚测试。
var theText =GOOGLE;
$(#HowYouKnow选项:contains(+ theText +))。attr('selected','selected');
或者,如果有相似的值(谢谢shanabus):
$每个(function(){
if($(this).text()== theText){
$(this).attr('selected','selected');
}
});
I have a dropdown as:
<select id="HowYouKnow" >
<option value="1">FRIEND</option>
<option value="2">GOOGLE</option>
<option value="3">AGENT</option></select>
In the above dropdown i know the text of the dropdown. How can set the value of the dropdown in document.ready with the text using jquery?
解决方案
This is a method that works based on the text of the option, not the index. Just tested.
var theText = "GOOGLE";
$("#HowYouKnow option:contains(" + theText + ")").attr('selected', 'selected');
Or, if there are similar values (thanks shanabus):
$("#HowYouKnow option").each(function() {
if($(this).text() == theText) {
$(this).attr('selected', 'selected');
}
});
这篇关于使用jquery通过文本设置下拉列表值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文