Pra*_*sad 84 jquery drop-down-menu
我有一个下拉列表:
<select id="HowYouKnow" >
<option value="1">FRIEND</option>
<option value="2">GOOGLE</option>
<option value="3">AGENT</option></select>
Run Code Online (Sandbox Code Playgroud)
在上面的下拉列表中,我知道下拉列表的文本.如何使用jquery使用文本设置document.ready中下拉列表的值?
Pet*_*r J 207
这是一种基于选项文本而不是索引的方法.刚刚测试过
var theText = "GOOGLE";
$("#HowYouKnow option:contains(" + theText + ")").attr('selected', 'selected');
Run Code Online (Sandbox Code Playgroud)
或者,如果有相似的值(感谢shanabus):
$("#HowYouKnow option").each(function() {
if($(this).text() == theText) {
$(this).attr('selected', 'selected');
}
});
Run Code Online (Sandbox Code Playgroud)
Par*_*ham 15
对于完全匹配使用
$("#HowYouKnow option").filter(function(index) { return $(this).text() === "GOOGLE"; }).attr('selected', 'selected');
Run Code Online (Sandbox Code Playgroud)
contains将选择最后一个可能不准确的匹配.
小智 5
$("#HowYouKnow option[value='" + theText + "']").attr('selected', 'selected'); // added single quotes
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
180436 次 |
最近记录: |