网上搜了一下,没有找到.net中选择radiobuttonlist控件的好方法 因此,我向每个单选按钮列表添加一个类,并使用类选择器循环每个单选按钮,但是,似乎每当一个更改时,所有更改都会更改。看我的代码如下: 这是 jQuery 部分:
function Checkform() {
result=true;
$('.rbl').each(function() {
var cc = $(this + "[checked]").val();
if (cc == undefined) {
result = false;
return false;
}
});
return result;
}
这是网络部件:
<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>
我想要做的是在提交表单之前检查所有单选按钮列表控件是否都有其选定的值。但它的工作原理就像一个人选择了值,无论另一个人选择了值,该函数都会返回 true。请帮助我解决这个问题。预先感谢您。
最佳答案
这个怎么样:
function Checkform() {
var result = true;
$('.rbl').each(function() {
var checked = $(this).find('input:radio:checked');
if (checked.length == 0) {
result = false;
return;
}
});
return result;
}
这将检查每个组并确定该组中是否选择了单选按钮。键是 $(this).find('..')
,它返回当前组中所有“选中”的单选按钮,如果没有选择,则返回 0,如果选择一个,则返回 1。
关于jquery - 如何循环浏览单选按钮列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5655461/