Alessandro Minneci
Alessandro Minneci

Reputation: 301

How to Check if RadioButton-Item is selected after PageLoad?

My RadioButtonList is created with pure asp.net like this:

<asp:RadioButtonList ID="RadioButtonListGutscheinArt" Visible="true" runat="server" ClientIDMode="Static">
     <asp:ListItem ID="ListItemZugAbonnement" ClientIDMode="Static" Value="1" />
     <asp:ListItem ID="ListItemBestellungHalbtax" ClientIDMode="Static" Text="Bestellung Halbtax" Value="2" />
</asp:RadioButtonList>

When my page reloads, the ListItemBestellungHalbtax will be selected. Now i want to check if it's actually selected with javascript. I tried it like this:

    if ($('#ListItemBestellungHalbtax').is(":checked")) 
    {
         //do whatever
    }

With no success.. Any suggestions?

Maybe it doesnt work, because the ListItem-Selection is not "saved". Even tough it is clearly visible that it's selected....

Upvotes: 2

Views: 519

Answers (1)

Offir
Offir

Reputation: 3491

When you create:

<asp:RadioButtonList ID="RadioButtonListGutscheinArt" Visible="true" runat="server" ClientIDMode="Static">
     <asp:ListItem ID="ListItemZugAbonnement" ClientIDMode="Static" Value="1" />
     <asp:ListItem ID="ListItemBestellungHalbtax" ClientIDMode="Static" Text="Bestellung Halbtax" Value="2" />
</asp:RadioButtonList>

It actually does this:

<table id="RadioButtonListGutscheinArt" border="0">
    <tbody><tr>
        <td><span clientidmode="Static" id="ListItemZugAbonnement"><input id="RadioButtonListGutscheinArt_0" type="radio" name="ctl00$main$RadioButtonListGutscheinArt" value="1"><label for="RadioButtonListGutscheinArt_0">1</label></span></td>
    </tr><tr>
        <td><span clientidmode="Static" id="ListItemBestellungHalbtax"><input id="RadioButtonListGutscheinArt_1" type="radio" name="ctl00$main$RadioButtonListGutscheinArt" value="2"><label for="RadioButtonListGutscheinArt_1">Bestellung Halbtax</label></span></td>
    </tr>
</tbody></table>

So change your condition to this:

 if ($('#ListItemBestellungHalbtax input').is(":checked")) {
        //do whatever
    }

and it suppose to work.

Upvotes: 4

Related Questions