javascript RadioButtonList 選択値

【ASPXソース】
<%-- クリックするとアラート表示--%>
<input id="button1" type="button" value="選択表示" 
  onclick="getCheckedRadio('<%= RadioButtonList1.ClientID %>')"/>

<%-- ラジオボタンリスト--%>
<asp:RadioButtonList ID="RadioButtonList1" runat="server">
  <asp:ListItem>AAA</asp:ListItem>
  <asp:ListItem Selected="True">BBB</asp:ListItem>
  <asp:ListItem>CCC</asp:ListItem>
</asp:RadioButtonList>


【javascript(jQuery)ソース】
//この引数idはコントロールIDの文字列を渡してください。
function getCheckedRadio(id) {

  //各Itemの配列を取得
  //↓javascriptならこれ
  var radio = document.getElementById(id).getElementsByTagName("input");

  //↓jQueryならこれ
  //var radio = $("#" + id + " :radio");

    for (var j = 0; j < radio.length; j++) {

       //選択されたラジオボタンの時
       if (radio[j].checked)
        
        //アラート表示
        alert(radio[j].value);

    }
}



これで、選択表示ボタンをクリックすると、「BBB」のアラートが表示されます。
これを応用して、いろいろできそう。
原文地址:https://www.cnblogs.com/haiy/p/4079147.html