如何遍历单选按钮列表

Ste*_*ack 9 asp.net jquery radiobuttonlist

我在网上搜索并没有找到一个很好的方法来选择.net中的radiobuttonlist控件所以我在每个radiobuttonlist中添加一个类,并使用类选择器循环每个,但是,似乎只要一个更改,所有更改.请参阅我的代码如下:这是jQuery部分:

function Checkform() {
    result=true;
    $('.rbl').each(function() {
            var cc = $(this + "[checked]").val();
            if (cc == undefined) {
                result = false;
                return false;
            }
        });
        return result;
    }
Run Code Online (Sandbox Code Playgroud)

这是网络部分:

<form id="form1" runat="server" onsubmit="return Checkform();">
<asp:RadioButtonList ID="RadioButtonList1" class="rbl"  runat="server">
        <asp:ListItem>1</asp:ListItem>
        <asp:ListItem>2</asp:ListItem>
        <asp:ListItem>3</asp:ListItem>
    </asp:RadioButtonList>

    <asp:RadioButtonList ID="RadioButtonList2" class="rbl" runat="server" 
        RepeatDirection="Horizontal">
        <asp:ListItem>1</asp:ListItem>
        <asp:ListItem>2</asp:ListItem>
        <asp:ListItem>3</asp:ListItem>
        <asp:ListItem>4</asp:ListItem>
    </asp:RadioButtonList>
Run Code Online (Sandbox Code Playgroud)

我想要做的是在提交表单之前检查所有radiobuttonlist控件是否具有其选定值.但它的工作方式就像一个人选择了值,无论天气另一个人选择了值,该函数都会返回true.请帮我解决这个问题.先感谢您.

fre*_*edw 12

这个怎么样:

function Checkform() {
    var result = true;
    $('.rbl').each(function() {
        var checked = $(this).find('input:radio:checked');
        if (checked.length == 0) {
            result = false;
            return;
        }
    });
    return result;
}
Run Code Online (Sandbox Code Playgroud)

这将检查每个组并确定是否在该组中选择了radiobuttom.键是$(this).find('..')返回当前组中所有"已检查"的单选按钮,如果没有选择则返回零,如果选择了1,则返回1.