本文实例讲述了js简单操作select和dropdownlist的方法。分享给大家供大家参考。具体实现方法如下:
一、js选中服务器控件select与dropdownlist
1. js操作服务器控件select
复制代码 代码如下:<select id=selectid onchange=return showmessage()>
<option value=0>==请选择==</option>
<option value=1>是</option>
<option value=2>否</option>
</select>
<script type=text/javascript language=javascript>
function showmessage() {
if (document.getelementbyid(selectid).options[document.getelementbyid(selectid).selectedindex].value == 1) {
alert(你好,你选择了第 1 个);
document.getelementbyid(txtcontractname).setattribute(enable,false);
}
else if (document.getelementbyid(selectid).options[document.getelementbyid(selectid).selectedindex].value == 2) {
alert(你好,你选择了第 2 个);
}
}
</script>
//js操作服务器控件dropdownlist
<asp:dropdownlist id=ddlfolder runat=server skinid=ddlskin autopostback=false onselectedindexchanged=ddlfolder_selectedindexchanged>
<asp:listitem value=0>选项0</asp:listitem>
<asp:listitem value=1>选项1</asp:listitem>
</asp:dropdownlist>
<asp:dropdownlist id=ddlfolder runat=server skinid=ddlskin autopostback=false onselectedindexchanged=ddlfolder_selectedindexchanged>
<asp:listitem value=0>选项0</asp:listitem>
<asp:listitem value=1>选项1</asp:listitem>
</asp:dropdownlist>
js代码:
复制代码 代码如下:document.getelementbyid(ddlfolder).value=0;//0为你要选中的项的value
2. 根据text值设置选中某项
复制代码 代码如下:var dropdownlistcurrencynew = document.getelementbyid(ddlfolder);
for(i = 0; i < dropdownlistcurrencynew.options.length; i++)
{
if(dropdownlistcurrencynew.options[i].text == 选项0)
{
dropdownlistcurrencynew.options[i].selected = true;
}
}
二、js读取dropdownlist选中项的value和text
value:
复制代码 代码如下:var selvalue = document.getelementbyid(dropdownlist1).options[document.getelementbyid(dropdownlist1).selectedindex].value;
text:
复制代码 代码如下:var seltext = document.getelementbyid(dropdownlist1).options[document.getelementbyid(dropdownlist1).selectedindex].text;
希望本文所述对大家的js与.net程序设计有所帮助。