0
 <asp:RadioButton Text="Male" value="M" ID="rbM" runat="server" GroupName="g" />
    <asp:RadioButton Text="Female" value="F" ID="rbF" runat="server" GroupName="g" />
    <asp:Button Text="Submit" runat="server" ID="btn" />

これが私のJSです:

 $(document).ready(function () {

            $("#<%=btn.ClientID%>").click(function () {

                if ($("input[id='rbM']").is(":checked") == true) {
                alert($(this).next('label').text());

                }
                else if ($("input[id='rbF']").is(":checked") == true) {

                }
                else  {
                    alert("Please select an option!");
                }
            });

        });

これは単純なラジオボタンの検証です。最初の 2 つの if 条件でテキストを取得するにはどうすればよいですか?最初の if のアラートで未定義になっていますか?

4

5 に答える 5

0

これを試して

$(document).ready(function () {

    $("#<%=btn.ClientID%>").click(function () {

        if ($("input[id='rbM']").is(":checked") == true) {

            alert($("input[id='rbM']").val());
        }
        else if ($("input[id='rbF']").is(":checked") == true) {
            alert($("input[id='rbF']").val());
        }
        else  {
            alert("Please select an option!");
        }
    });

});
于 2013-07-30T08:54:07.407 に答える
0

以下を試してください

if ($("#rbM").is(":checked")) { // return true/false
} 
else  if ($("#rbF").is(":checked")) {
}
else {
  alert("Please select an option!");
}               
于 2013-07-30T08:54:34.590 に答える
0

そんな感じ

$(document).ready(function () {
    $("#<%=btn.ClientID%>").click(function () {
        if ($("input[id='rbM']").is(":checked") == true) {
            console.log($("input[id='rbm']").attr('Text'))
        }
        else if ($("input[id='rbF']").is(":checked") == true) {
            console.log($("input[id='rbF']").attr('Text'))
        }
        else  {
            alert("Please select an option!");
        }
    });
});
于 2013-07-30T08:52:54.810 に答える