DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> 關於JavaScript >> JS簡單操作select和dropdownlist實例
JS簡單操作select和dropdownlist實例
編輯:關於JavaScript     

本文實例講述了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程序設計有所幫助。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved