2013-03-07 42 views
0

在ASP下拉列表中選擇項目我有一個asp下拉列表:得到的jQuery

<asp:DropDownList runat="server" ID="select_install_type" name="select install type"> 
          <asp:ListItem Values="0" id ="installTypeMeterAndTransmitter" /> 
          <asp:ListItem Values="1" id ="installTypeTransmitterOnly" /> 
          <asp:ListItem Values="2" id="installTypeMeterOnly"/> 
         </asp:DropDownList> 

其項目我poplate在服務器端,用本地化名稱:

private void FillInstallTypeControl() 
     { 
      this.select_install_type.Items[0].Text = CultureResourceHelper.GetConst("meter_and_transmitter"); 
      this.select_install_type.Items[1].Text = CultureResourceHelper.GetConst("transmitter_only"); 
      this.select_install_type.Items[2].Text = CultureResourceHelper.GetConst("meter_only"); 
     } 

現在我想寫一個簡單的jquery方法,當選擇改變時運行, 並根據新選擇做點事情:

  $('#select_install_type').change(function() { 

       select_install_type. 
       var selected = ($("#select_install_type").val()); 
       } 

我可以選擇的值,但由於它是局部的,我不希望其對一些字符串文本比較喜歡這樣:

if (selected == "אאאא") 
{ //Do something 
} 

我怎麼能知道被選爲該項目的?也許得到選定的索引或ID? 但我是jquery的新手,所以我不知道該怎麼做。

非常感謝提前。

回答

3

嘗試:

$('#' + <%= select_install_type.ClientID %>).change(function() { 
      var selected = $(this).val(); 
    } 

或者你可以選擇的選項的ID:

$('#' + <%= select_install_type.ClientID %>).change(function() { 
      var selected = $(this).find('option:selected').prop("id"); 
    } 

jQuery Sample

+0

非常感謝,第二個選項工作:),我想第一個選項與我寫的代碼基本相同,對嗎? – 2013-03-07 13:38:46

+0

是的..我剛纔說的選項.. :) – Anujith 2013-03-07 13:45:55