Sel为你的下拉列表框,Value为你要的值
function setSelect(Sel,Value){
//在所有Option中查找Value,然后设为当前选择 if (typeof(Sel)!='object') return;//没有对象 //alert(Value);
var iInt = Sel.selectedIndex;
for (i=0; i<Sel.length;i++){
Sel.selectedIndex = i ;
if (Sel.value==Value){
break;
}
else {
Sel.selectedIndex=iInt;
}
}
}
function setSelect(Sel,Value){
//在所有Option中查找Value,然后设为当前选择 if (typeof(Sel)!='object') return;//没有对象 //alert(Value);
var iInt = Sel.selectedIndex;
for (i=0; i<Sel.length;i++){
Sel.selectedIndex = i ;
if (Sel.value==Value){
break;
}
else {
Sel.selectedIndex=iInt;
}
}
}
<body>
<select id=ddl>
<option value="a">a</option>
<option value="b">b</option>
</select>
<button onclick="ddl.value='b'">test</button>
</body>
</html>
<html>
<body>
<select id=ddl>
<option value="1">a</option>
<option value="2">b</option>
</select>
<button onclick="selectByText('b')">test</button>
</body>
<SCRIPT LANGUAGE="JavaScript">
<!--
function selectByText(str)
{
with (ddl)
for (var i=0;i<options.length;i++)
if (options[i].innerText==str)
options[i].selected=true
}
//-->
</SCRIPT>
</html>
if (options[i].innerText==str)
===>
if (options[i].text==str)if you want to do it on the server side, try something likefor (int i=0; i < DropDownList1.Items.Count; i++)
{
if (DropDownList1.Items[i].Text.Equals("b"))
{
DropwDownList1.SelectedIndex = i;
break;
}
}
DropwDownList1.value="b"就可以了.
用text有的时候访问不到,不知是什么原因,所以我一直用innerText