JS simple operation select and dropdownlist instances

  • 2020-03-30 04:22:11
  • OfStack

This article illustrates how JS can easily operate select and dropdownlist. Share with you for your reference. The specific implementation method is as follows:

Js select server control select and dropdownlist

1. Js operation server control select

<select id="selectID" onchange="return showMessage()">
     <option value="0">== Please select a ==</option>
     <option value="1"> is </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 no 1 a ");
   document.getElementById("txtContractName").setAttribute("enable",false);
      }
      else if (document.getElementById("selectID").options[document.getElementById("selectID").selectedIndex].value == 2) {
   alert(" Hello, you chose no 2 a ");
      }
  }
</script> //Js operation server control dropdownlist
<asp:DropDownList ID="ddlFolder" runat="server" SkinID="ddlSkin" AutoPostBack="false" OnSelectedIndexChanged="ddlFolder_SelectedIndexChanged">   
        <asp:ListItem Value="0"> options 0</asp:ListItem>  
   <asp:ListItem Value="1"> options 1</asp:ListItem> 
</asp:DropDownList>   
<asp:DropDownList ID="ddlFolder" runat="server" SkinID="ddlSkin" AutoPostBack="false" OnSelectedIndexChanged="ddlFolder_SelectedIndexChanged">
  <asp:ListItem Value="0"> options 0</asp:ListItem>
  <asp:ListItem Value="1"> options 1</asp:ListItem>
</asp:DropDownList>

JS code:
document.getElementById("ddlFolder").value="0";//0 For the item you want to select value

2. Select an item based on the Text value setting

var DropDownListCurrencyNew =  document.getElementById("ddlFolder");
for(i = 0; i < DropDownListCurrencyNew.options.length; i++)
{
          if(DropDownListCurrencyNew.options[i].text == " options 0")   
           {
               DropDownListCurrencyNew.options[i].selected = true;
          }
}

Ii. Js reads the value and text of the selected item in DropDownList

Value:

var selValue = document.getElementById("DropDownList1").options[document.getElementById("DropDownList1").selectedIndex].value;

Text:

var selText = document.getElementById("DropDownList1").options[document.getElementById("DropDownList1").selectedIndex].text;


Related articles: