2017-09-01 80 views
1

我保持一些舊的C#代碼,並有一個asp:單選按鈕列表...獲取屬性:單選按鈕列表使用jQuery

<asp:RadioButtonList AutoPostBack="false" TextAlign="Right" RepeatDirection="Horizontal" RepeatLayout="Flow" runat="server" ID="radIncidentType" />

...這是在代碼後面填充像這樣:

public static void PopulateRBL(string ListName, ref RadioButtonList ListToPopulate) 
    { 
     List<Lookup> radioList = GetLookup(ListName); //returns 
     foreach (Lookup entry in radioList) 
     { 
      ListItem item = new ListItem(" " + entry.Description + " ", entry.Code); 
      item.Attributes.Add("ItemId", entry.Id.ToString()); 
      ListToPopulate.Items.Add(item); 
     } 
    } 

因此增加每個項目都有說明,代碼,和一個額外的屬性ItemId

然後在onChange上執行一些驗證,它必須詢問ItemId屬性。目前正在做這樣的:

$('#ctl00_cphPage_radIncidentType input[type=radio]:checked').closest('span').attr('ItemId') 

,直到我說一個嵌套母版,並得到它的工作,我不得不選擇改變這是工作的罰款:

$('#ctl00_ctl00_cphPage_nestedPage_radIncidentType input[type=radio]:checked').closest('span').attr('ItemId') 

很明顯,我想一個整潔的選擇,我已經試過:

$('#<%= radIncidentType.ClientID %> input[type=radio]:checked').closest('span').attr('ItemId') 

和...

$("input[name='<%=radIncidentType.UniqueID%>']:radio:checked").closest('span').attr('ItemId') 

...但都沒有工作。任何人都可以提出一種獲得該屬性值的方法嗎?

+1

你嘗試用ID結尾的方法:'$( 「[ID $ = '_ radIncidentType']」)'和類似的理由? –

+0

謝謝,是的,嘗試過這一點,它現在正在工作 – atamata

回答

0

我嘗試了以下內容和它的作品根據需要...

$("[id$='_radIncidentType'] input[type=radio]:checked").closest('span').attr("ItemId") 
0

當您想要ItemId的值時,不是很清楚。但是在這個片段中,一個監聽器被添加到RadioButtonList中,並在單擊RadioButton時獲取該屬性。

或者從外部觸發的函數從選定的RadioButton獲取屬性。

<script type="text/javascript"> 
    function getItemIdFromRadioButtonList() { 
     $('#<%= radIncidentType.ClientID %> input[type="radio"]:checked').each(function() { 
      var ItemId = $(this).closest('span').attr("ItemId"); 
      alert(ItemId); 
     }); 
    } 
</script> 
+0

感謝您的回答,我嘗試了'結束'選擇器如上所述,這現在工作 – atamata