编辑: 什么不起作用:-我有两列(全部批准/全部拒绝)我如何限制用户只允许一个复选框?如果您不使用gridview,则以下代码有效....
我在这里问过这个问题(只允许选中一个(批准/拒绝)复选框)并且只有当我有 asp.net 控件含义而不使用 gridview 控件时它才能按预期工作,现在我处于我有的情况使用 gridview 控件,似乎我的代码不起作用......我保持了相同的类名。有什么帮助吗?
这是我的带有gridview的.aspx代码:
<script type="text/javascript" language="javascript">
$(document).ready(function () {
$('#C1All').click(function () { debugger
$('.col1 > input').attr("checked", $('#C1All').attr("checked"));
$('.col2 > input').removeAttr("checked");
$('#C2All').removeAttr("checked");
});
$('#C2All').click(function () { debugger
$('.col2 > input').attr("checked", $('#C2All').attr("checked"));
$('.col1 > input').removeAttr("checked");
$('#C1All').removeAttr("checked");
});
$('.col1').each(function () {
$(this).click(function () { debugger
var id = $("input", this).attr('id');
var coresId = id.replace('C1', 'C2');
$('#' + coresId).removeAttr("checked");
$('#C1All').removeAttr("checked");
$('#C2All').removeAttr("checked");
});
});
$('.col2').each(function () {
$(this).click(function () {debugger
var id = $("input", this).attr('id');
var coresId = id.replace('C2', 'C1');
$('#' + coresId).removeAttr("checked");
$('#C1All').removeAttr("checked");
$('#C2All').removeAttr("checked");
});
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gv" runat="server" AutoGenerateColumns="False" DataKeyNames="Id"
OnRowDataBound="gv_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Approve" HeaderStyle-HorizontalAlign="Center" HeaderStyle-Width="50px" >
<HeaderTemplate>
Approve<br />
<asp:CheckBox ID="C1All" runat="server" />
</HeaderTemplate>
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate >
<asp:CheckBox CssClass="col1" ID="chkApprove" runat="server" >
</asp:CheckBox>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Reject" HeaderStyle-HorizontalAlign="Center" HeaderStyle-Width="180px">
<HeaderTemplate>
Reject<br />
<asp:CheckBox ID="C2All" runat="server" />
<asp:DropDownList ID="drpPaymentMethod" runat="server">
<asp:ListItem Value="-1">Please select</asp:ListItem>
<asp:ListItem Value="0">Month</asp:ListItem>
<asp:ListItem Value="1">At End</asp:ListItem>
<asp:ListItem Value="2">At Travel</asp:ListItem>
</asp:DropDownList>
</HeaderTemplate>
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<div class="selectReason">
<asp:CheckBox CssClass="col2" ID="chkReject" runat="server" >
</asp:CheckBox>
<asp:DropDownList ID="drpPaymentMethod" runat="server">
<asp:ListItem Value="-1">Please select</asp:ListItem>
<asp:ListItem Value="0">Month</asp:ListItem>
<asp:ListItem Value="1">At End</asp:ListItem>
<asp:ListItem Value="2">At Travel</asp:ListItem>
</asp:DropDownList>
</div>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:BoundField DataField="ID" ControlStyle-Width="250px" HeaderText="ID" SortExpression="ID" />
<asp:BoundField DataField="FirstName" ControlStyle-Width="250px" HeaderText="FirstName"
SortExpression="FirstName" />
<asp:BoundField DataField="LastName" ControlStyle-Width="250px" HeaderText="LastName"
SortExpression="LastName" />
<asp:TemplateField>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>