简体   繁体   中英

Get selected value of radio button in Asp.net through javascript

I know it has been asked several times and I found many solutions but no solution worked for me.

I am working on IE

<asp:RadioButtonList ID="rbtnView" runat="server" OnSelectedIndexChanged="rbtnView_SelectedIndexChanged"
AutoPostBack="True" TabIndex="7" RepeatDirection="Horizontal">
    <asp:ListItem Selected="True" Value="0">Show Pending</asp:ListItem>
     <asp:ListItem Value="1">Show Processed</asp:ListItem>
 </asp:RadioButtonList>

here is the button code from where I am calling the function:

<asp:Button ID="btnPrint" runat="server" CssClass="myButton" OnClick="btnPrint_Click"
Text="Print" ValidationGroup="validationgroup2" TabIndex="9" OnClientClick="doPrint()" />

here is the javascrit function

function doPrint() {
    debugger;
    var rad = $('#<%=rbtnView.ClientID %> input[type=radio]:checked').val();
}

Any help would be appreciated.

var radioValue = $("input[name='YOURVALUEFORTHEGROUP']:checked").val();

替换您的价值

The javascript function works just fine. But you are doing a form post (PostBack) when pressing the btnPrint button.

You can use a regular button instead of an asp.net Control. Or if you need a PostBack to execute the btnPrint_Click method. But then you can get the value there. It all depends why you need the value client side.

<input type="button" value="Get value" onclick="doPrint()" />

Or add return false to the aspnet button.

OnClientClick="doPrint(); return false;"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM