JS簡單操作select和dropdownlist執行個體,selectdropdownlist

來源:互聯網
上載者:User

JS簡單操作select和dropdownlist執行個體,selectdropdownlist

本文執行個體講述了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程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.