我需要你的帮助。
我是javascript的新手,我不确定如何在选择框中搜索指定选项然后选择它
HTML:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<select id="select1">
<option value="apples">apples</option>
<option value="oranges">oranges</option>
<option value="bananas">bananas</option>
<option value="mangos">mangos</option>
<option value="pears">pears</option>
</select>
</body>
</html>
即
lookup("select1","mangos")
javascript逻辑:
function lookup("selectid","stringtosearchfor") {
look through the selected "selectid" and find the string "mangos"
if found { select mangos from the select box }
}
你是如何编码的?
提前致谢,
答案 0 :(得分:1)
function lookup(id, value)
{
var ret = undefined;
if(document.getElementById(id) != undefined)
{
var options = document.getElementById(id).childNodes;
for(i = 0; i < options.length; i++)
{
if(options[i].value == value)
{
ret = options[i];
break;
}
}
}
return ret;
}
答案 1 :(得分:1)
这比你想象的要容易......试试:
document.getElementById('select1').value = 'mangos';
答案 2 :(得分:1)
通过Jquery:
$('#select1').val('mangos');