I have a signup.aspx page with a radio button list
<asp:radiobuttonlist id="kita1" runat="server">
<asp:listitem id="option1" runat="server" value="first" />
<asp:listitem id="option2" runat="server" value="second" />
<asp:listitem id="Listitem1" runat="server" value="third" />
</asp:radiobuttonlist>
and I need to get the value of the selected item and save it to a variable
how can I do that with javascript ?
CodePudding user response:
<form runat="server">
<asp:RadioButtonList ID="RadioButtonList1" runat="server"
RepeatDirection="Horizontal" RepeatLayout="flow" >
<asp:ListItem Selected="True" Text ="Yes" Value="0"></asp:ListItem>
<asp:ListItem Text ="No" Value="1"></asp:ListItem>
</asp:RadioButtonList> </form>
<p id="button" onclick="getvalue()">click me</p>
<script type="text/javascript">
function getvalue(){
alert($('#<%=RadioButtonList1.ClientID %> input[type=radio]:checked').val());
}
</script>
CodePudding user response:
try this way
var list = document.getElementById("radios"); //Client ID of the radiolist
var inputs = list.getElementsByTagName("input");
var selected;
for (var i = 0; i < inputs.length; i ) {
if (inputs[i].checked) {
selected = inputs[i];
break;
}
}
if (selected) {
alert(selected.value);// use value
}