2

Can I have an example code for this? Two radio buttons in a group name "Gender". If the user selects nothing, a message should appear saying "select one". I've done some custom validations with textboxes and dropdownlists and I became stuck with radiobuttons.

4

2 回答 2

1
$("form").submit(function(e){

    e.preventDefault();

    if($(this).find("input[name=Gender]:checked").length === 0){
        alert('Gender not selected.');
    }

});

您还可以使用所选无线电组的 val() 来检查它是未定义还是值。jsfiddle:http: //jsfiddle.net/jh8p3/

asp.net 中的客户端验证

<asp:RadioButtonList ID="Gender" runat="server">
    <asp:ListItem Text="Male" Value="Male" />
    <asp:ListItem Text="Female" Value="Female" />
</asp:RadioButtonList>

<asp:Button ID="Button1"
    Text="Validate"
    runat="server" OnClientClick="return validate();" />
<script>
    function validate() {
        if (checkGender()) {
            return true;
        }
        return false;
    }
    function checkGender() {
        var selectedGenderRB = document.querySelector('#<%=Gender.ClientID%> input:checked');
        if (selectedGenderRB) {
            return true;
        } else {
            alert('gender not selected.');
            return false;
        }
    }
</script>
于 2013-08-31T03:08:15.053 回答
0

来个小小提琴怎么样

http://jsfiddle.net/bTf2R/

$('input[type=radio]').change(function(){
    $('p').text('Thanks');
    
});
于 2013-08-31T03:10:10.530 回答