使用 jquery 按文本设置下拉值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1888931/
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
set dropdown value by text using jquery
提问by Prasad
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?
在上面的下拉列表中,我知道下拉列表的文本。如何使用 jquery 使用文本在 document.ready 中设置下拉列表的值?
回答by Peter J
This is a method that works based on the textof 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):
或者,如果有相似的值(感谢 shanabus):
$("#HowYouKnow option").each(function() {
if($(this).text() == theText) {
$(this).attr('selected', 'selected');
}
});
回答by Parham
For the exact match use
对于精确匹配使用
$("#HowYouKnow option").filter(function(index) { return $(this).text() === "GOOGLE"; }).attr('selected', 'selected');
contains is going to select the last match which might not be exact.
contains 将选择最后一个可能不准确的匹配项。
回答by Vinay saini
$("#HowYouKnow option[value='" + theText + "']").attr('selected', 'selected'); // added single quotes
回答by Matthew Feerick
var myText = 'GOOGLE';
$('#HowYouKnow option').map(function() {
if ($(this).text() == myText) return this;
}).attr('selected', 'selected');
回答by Divy
try this..
尝试这个..
$(element).find("option:contains(" + theText+ ")").attr('selected', 'selected');
回答by juhi
For GOOGLE, GOOGLEDOWN, GOOGLEUP i.e similar kind of value you can try below code
对于 GOOGLE、GOOGLEDOWN、GOOGLEUP,即类似的值,您可以尝试以下代码
$("#HowYouKnow option:contains('GOOGLE')").each(function () {
if($(this).html()=='GOOGLE'){
$(this).attr('selected', 'selected');
}
});
In this way,number of loop iteration can be reduced and will work in all situation.
这样,可以减少循环迭代次数,并且在所有情况下都有效。
回答by stevensagaar
The below code works for me -:
下面的代码对我有用-:
jQuery('[id^=select_] > option').each(function(){
if (this.text.toLowerCase()=='text'){
jQuery('[id^=select_]').val(this.value);
}
});
jQuery('[id^=select_]')- This allows you to select drop down where ID of the drop down starts from select_
jQuery('[id^=select_]')- 这允许您选择下拉列表,其中下拉列表的 ID 从select_开始
Hope the above helps!
希望以上有帮助!
Cheers S
干杯
回答by fullstacklife
Here is an simple example:
这是一个简单的例子:
$("#country_id").change(function(){
if(this.value.toString() == ""){
return;
}
alert("You just changed country to: " + $("#country_id option:selected").text() + " which carried the value for country_id as: " + this.value.toString());
});
回答by dell
$("#HowYouKnow").val("GOOGLE");
回答by helloandre
$("#HowYouKnow option:eq(XXX)").attr('selected', 'selected');
where XXX is the index of the one you want.
其中 XXX 是您想要的索引。