2012-01-26 23 views
1

我把一對夫婦單選按鈕的在同一組中的方式如下:如何在javascript中訪問同一組中的單選按鈕?

<asp:RadioButton ID="ID11" Text="Text1" GroupName="G1" runat="server" /> 
<asp:RadioButton ID="ID12" Text="Text2" GroupName="G1" runat="server" /> 
<asp:RadioButton ID="ID13" Text="Text3" GroupName="G1" runat="server" /> 

ID的用於動態地生成的每個單選按鈕。 然後我想訪問這些單選按鈕在JavaScript中。如何在不使用每個ID的情況下通過這些單選按鈕?

+2

添加一個共同的類? – chchrist

+0

@Xander確實我在我的腦海裏有jQuery – chchrist

回答

0

好,這總是要產生於

<input type="radio" /> 

控制

想象他們是ID爲 '形式' 形式你會做這樣的事情:

var form = document.getElementById('form'); 
var controls = form.getElementsByTagName('input'); 
for(var control in controls){ 
    if(control.getAttribute('type') == 'radio'{ 
     //control will be a radiobutton 
    } 
} 
1

你應該使用RadioButtonList

<asp:RadioButtonList id="rblOption" runat="server"> 
    <asp:ListItem Value="1" Text="Yes" /> 
    <asp:ListItem Value="0" Text="No" /> 
</asp:RadioButtonList> 

,你可以使用類似的東西獲得價值

<script type="text/javascript"> 
     var rblOption= '<%= rbList.ClientID %>'; 
    </script> 
相關問題