如何从4个CheckBox中检查任何2个CheckBox? [英] how to Check any 2 checkBox from 4 CheckBoxes?
本文介绍了如何从4个CheckBox中检查任何2个CheckBox?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Good Afternoon Developer,这是我在asp .net中的内容,我希望用户只能选中2个复选框,并且超出警告框.
Good Afternoon Developer, this is my content in asp .net, I want the user can check only 2 checkbox,exceeding that alert box should pop up.
<body>
<form id="form1" runat="server">
<div>
<table width="100%">
<tr>
<td>
<asp:CheckBox id="q3a" runat="server" Text="Public" />
</td>
<td>
<asp:CheckBox id="q3b" runat="server" Text="void" />
</td>
<td>
<asp:CheckBox id="q3c" runat="server" Text="protected"/>
</td>
<td>
<asp:CheckBox id="q3d" runat="server" Text="return" />
</td>
</tr>
</table>
<asp:Button ID="btnSubmit" Text="submit" runat="server"/>
</div>
</form>
</body>
我如何为此编写JavaScript,我已经尝试过但是找不到任何方法,请帮助...
how can i write javascript for this, i have tried but can''t find any way out, Pls help...
推荐答案
<asp:CheckBoxList ID="CheckBoxList1" runat="server" >
<asp:ListItem Text = "One" Value = "1"></asp:ListItem>
<asp:ListItem Text = "Two" Value = "2"></asp:ListItem>
<asp:ListItem Text = "Three" Value = "3"></asp:ListItem>
<asp:ListItem Text = "Four" Value = "4"></asp:ListItem>
</asp:CheckBoxList>
<asp:Button ID="btn" runat="server" OnClientClick="return Validate()" Text="Click" />
<script language="javascript" type="text/javascript">
function Validate() {
var CHK = document.getElementById("<%=CheckBoxList1.ClientID%>");
var checkbox = CHK.getElementsByTagName("input");
var counter = 0;
for (var i = 0; i < checkbox.length; i++) {
if (checkbox[i].checked) {
counter++;
}
}
if (counter > 2) {
alert("You can select maximum 2 checkboxes");
return false;
}
else {
return true;
}
//alert(counter);
}
</script>
funtion CheckValidation()
{
var chk1=document.getElementById(''<%=q3a.ClientID%>'');
var chk2=document.getElementById(''<%=q3a.ClientID%>'');
var chk3=document.getElementById(''<%=q3a.ClientID%>'');
var chk4=do cument.getElementById(''<%=q3a.ClientID%>'');
var count=0;
if(chk1.checked)
{
count++;
}
if(chk2.checked)
{
count++;
}
if(chk3.checked)
{
count++;
}
if(chk4.checked)
{
count++;
}
if(count>2)
{
//do your logic here
}
}
call this funtion onselect /onchange event on the checkbox
<asp:checkbox id="q3a" runat="server" onchange="CheckValidation()" text="Public" xmlns:asp="#unknown" />
这篇关于如何从4个CheckBox中检查任何2个CheckBox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文