本文實(shí)例講述了js簡(jiǎn)單操作select和dropdownlist的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
一、js選中服務(wù)器控件select與dropdownlist
1. js操作服務(wù)器控件select
復(fù)制代碼 代碼如下:<select id=selectid onchange=return showmessage()>
<option value=0>==請(qǐng)選擇==</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 個(gè));
document.getelementbyid(txtcontractname).setattribute(enable,false);
}
else if (document.getelementbyid(selectid).options[document.getelementbyid(selectid).selectedindex].value == 2) {
alert(你好,你選擇了第 2 個(gè));
}
}
</script>
//js操作服務(wù)器控件dropdownlist
<asp:dropdownlist id=ddlfolder runat=server skinid=ddlskin autopostback=false onselectedindexchanged=ddlfolder_selectedindexchanged>
<asp:listitem value=0>選項(xiàng)0</asp:listitem>
<asp:listitem value=1>選項(xiàng)1</asp:listitem>
</asp:dropdownlist>
<asp:dropdownlist id=ddlfolder runat=server skinid=ddlskin autopostback=false onselectedindexchanged=ddlfolder_selectedindexchanged>
<asp:listitem value=0>選項(xiàng)0</asp:listitem>
<asp:listitem value=1>選項(xiàng)1</asp:listitem>
</asp:dropdownlist>
js代碼:
復(fù)制代碼 代碼如下:document.getelementbyid(ddlfolder).value=0;//0為你要選中的項(xiàng)的value
2. 根據(jù)text值設(shè)置選中某項(xiàng)
復(fù)制代碼 代碼如下:var dropdownlistcurrencynew = document.getelementbyid(ddlfolder);
for(i = 0; i < dropdownlistcurrencynew.options.length; i++)
{
if(dropdownlistcurrencynew.options[i].text == 選項(xiàng)0)
{
dropdownlistcurrencynew.options[i].selected = true;
}
}
二、js讀取dropdownlist選中項(xiàng)的value和text
value:
復(fù)制代碼 代碼如下:var selvalue = document.getelementbyid(dropdownlist1).options[document.getelementbyid(dropdownlist1).selectedindex].value;
text:
復(fù)制代碼 代碼如下:var seltext = document.getelementbyid(dropdownlist1).options[document.getelementbyid(dropdownlist1).selectedindex].text;
希望本文所述對(duì)大家的js與.net程序設(shè)計(jì)有所幫助。