使用Javascript的ASP.NET复选框列表验证
ASP.NET Checkboxlist validation using Javascript
我有一个asp.net复选框列表,如下所示:
<asp:CheckBoxList ID="cblCurrency" runat="server">
<asp:ListItem Text="ALL" Value="0"></asp:ListItem>
<asp:ListItem Text="A" Value="1"></asp:ListItem>
<asp:ListItem Text="B" Value="2"></asp:ListItem>
<asp:ListItem Text="C" Value="3"></asp:ListItem>
<asp:ListItem Text="D" Value="4"></asp:ListItem>
</asp:CheckBoxList>
要求是,如果选择"ALL",则应从所有其他复选框中删除选中的属性,如果选择任何其他复选框,则应删除"ALL"的选中属性。我想使用javascript实现这一点,但最好使用jQuery。如果你知道怎么做,请回复。我们将不胜感激。
这是我的解决方案:
$("#<%=cblCurrency.ClientID%> input").click(function () {
if ($('<%= "#" + cblCurrency.ClientID + "_0" %>').is(":checked")) {
$("#<%=cblCurrency.ClientID%> input:checkbox").not(this).prop("checked", false);
}
});
基本上,我们使用由ASP.NET生成的复选框的索引,该索引在末尾的ID属性中使用。与此类似:cblCurrency_1
,意思是索引1处的复选框。
试试这个:
标记:
<asp:CheckBoxList ID="cblCurrency" runat="server" CssClass="CheckBoxList" ClientIDMode="Static">
<asp:ListItem Text="ALL" Value="0"></asp:ListItem>
<asp:ListItem Text="A" Value="1"></asp:ListItem>
<asp:ListItem Text="B" Value="2"></asp:ListItem>
<asp:ListItem Text="C" Value="3"></asp:ListItem>
<asp:ListItem Text="D" Value="4"></asp:ListItem>
</asp:CheckBoxList>
JavaScript:
$(document).ready(function () {
$(".CheckBoxList input:checkbox:first").change(function () {
$(".CheckBoxList").find(':checkbox').prop("checked", this.checked);
});
$(".CheckBoxList input:checkbox:not(first)").change(function () {
$(".CheckBoxList").find(':checkbox:first').prop("checked", this.checked);
});
});
创建一个事件处理程序,用于管理所有复选框上的单击,并捕获所需的用户行为。
如果您获取这些代码,并在呈现的HTML上查看源代码,我们将得到:
<table id="MainContent_cblCurrency">
<tr>
<td><input id="MainContent_cblCurrency_0" type="checkbox" name="ctl00$MainContent$cblCurrency$0" value="0" /><label for="MainContent_cblCurrency_0">ALL</label></td>
</tr><tr>
<td><input id="MainContent_cblCurrency_1" type="checkbox" name="ctl00$MainContent$cblCurrency$1" value="1" /><label for="MainContent_cblCurrency_1">A</label></td>
</tr><tr>
<td><input id="MainContent_cblCurrency_2" type="checkbox" name="ctl00$MainContent$cblCurrency$2" value="2" /><label for="MainContent_cblCurrency_2">B</label></td>
</tr><tr>
<td><input id="MainContent_cblCurrency_3" type="checkbox" name="ctl00$MainContent$cblCurrency$3" value="3" /><label for="MainContent_cblCurrency_3">C</label></td>
</tr><tr>
<td><input id="MainContent_cblCurrency_4" type="checkbox" name="ctl00$MainContent$cblCurrency$4" value="4" /><label for="MainContent_cblCurrency_4">D</label></td>
</tr>
</table>
它不漂亮,但这是我们必须要做的。
此JS将捕获所有复选框的点击。它根据包含表的ID来定位复选框。如果选中了"全部"复选框(基于选中输入的值),则将取消选中当前选中的所有复选框:
$().ready(
function () {
$("#<%=cblCurrency.ClientID%> input").click( // The ClientID property should tell you that the ID if the table is
function () {
var eventSource = $(this); //The checkbox that was clicked
//for debugging Delete once you get working
//console.log(eventSource.val())
//console.log(eventSource.is(':checked'))
if (eventSource.val() == "0" && eventSource.is(':checked')) { // Make sure the value is what you expect and that the check box is being checked (and not unchecked)
$("#<%=cblCurrency.ClientID%> input").not(eventSource).prop('checked', false)// uncheck all other checkbox. exclude the input that is the source of the event.
}
}
)
}
);
这里有一个版本:http://jsfiddle.net/RGW4Q/
相关文章:
- 如何保持引导程序下拉复选框列表下拉
- (取消)使用单独的复选框激活复选框列表
- 复选框列表在Knockout JS中不起作用
- 如何创建至少 1 个框处于活动状态的复选框列表
- 如何将从谷歌自定义搜索API获取的数据放入复选框列表
- 检查多个复选框列表与单击一个按钮
- Angular 2:用HTTP响应填充复选框列表
- 使用复选框列表中的jquery附加文本
- 如何部分隐藏asp.net复选框列表项
- 使用DOM创建并填充一个带有javascript数组值的复选框列表
- jQuery可搜索复选框列表和三态复选框
- 在php生成的页面上获取勾选复选框列表的动态方式
- AngularJS:用两个JSON数组填充复选框列表,最初检查了一个数组中的一些项
- 项目的复选框列表的总和
- 使用jQuery从复选框列表中获取所选项目
- 如何在复选框列表项选择上调用 javascript 函数
- 更改复选框列表项样式
- AngularJS不保留表单中复选框列表中的选定项
- 当用户选中复选框列表中的复选框之一时,我们如何禁用复选框
- 复选框列表 将数组格式化为 JSON 并在 ng-repeat 中映射后未填充