This article describes how JS can operate select and dropdownlist in simple operation. Share it for your reference. The specific implementation method is as follows:
1. Select server controls select and dropdownlist by js
1. js operation server control select
Copy the code as follows: <select id="selectID" onchange="return showMessage()">
<option value="0">==Please select ==</option>
<option value="1">Yes</option>
<option value="2">No</option>
</select>
<script type="text/javascript" language="javascript">
function showMessage() {
if (document.getElementById("selectID").options[document.getElementById("selectID").selectedIndex].value == 1) {
alert("Hello, you chose the first one");
document.getElementById("txtContractName").setAttribute("enable",false);
}
else if (document.getElementById("selectID").options[document.getElementById("selectID").selectedIndex].value == 2) {
alert("Hello, you chose the second one");
}
}
</script>
//js operation server control dropdownlist
<asp:DropDownList ID="ddlFolder" runat="server" SkinID="ddlSkin" AutoPostBack="false" OnSelectedIndexChanged="ddlFolder_SelectedIndexChanged">
<asp:ListItem Value="0">Option 0</asp:ListItem>
<asp:ListItem Value="1">Option 1</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ddlFolder" runat="server" SkinID="ddlSkin" AutoPostBack="false" OnSelectedIndexChanged="ddlFolder_SelectedIndexChanged">
<asp:ListItem Value="0">Option 0</asp:ListItem>
<asp:ListItem Value="1">Option 1</asp:ListItem>
</asp:DropDownList>
JS code:
The code copy is as follows: document.getElementById("ddlFolder").value="0";//0 is the value of the item you want to select
2. Select an item according to the Text value setting
The code copy is as follows: var DropDownListCurrencyNew = document.getElementById("ddlFolder");
for(i = 0; i < DropDownListCurrencyNew.options.length; i++)
{
if(DropDownListCurrencyNew.options[i].text == "Option 0")
{
DropDownListCurrencyNew.options[i].selected = true;
}
}
2. js read the value and text of the selected item of DropDownList
Value:
The code copy is as follows: var selValue = document.getElementById("DropDownList1").options[document.getElementById("DropDownList1").selectedIndex].value;
Text:
The code copy is as follows: var selText = document.getElementById("DropDownList1").options[document.getElementById("DropDownList1").selectedIndex].text;
I hope this article will be helpful to everyone's js and .net programming.