本文實(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ì)有所幫助。
更多信息請(qǐng)查看IT技術(shù)專欄