遍历啊。 var counter=0; var checks = document.getElementsByName("XXX"); for(var i in checks){ if(checks[i].checked==true) counter++; } if(counter<10) {alert(XXX),return false;}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>checkbox.html</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <script type="text/javascript"> function checkNum() { var box = document.getElementsByName("v__id__8"); var count = 0; for (var i = 0; i < box.length; i++) { if (box[i].checked) { count ++; } } if (count < 10) { alert("你选择的数量小于10个!"); } }
if(n<10)
{
alert("請至少選擇10個!");
return false;
}jQuery
var ckbselect;
for(var i = 0; i<ckblist.length;i++)
{
if(ckblist[i].checked)
{
ckbselect[i] = ckblist[i]
}
}
if(ckblist.length <10)
{
alert('请输入')
}
var counter=0;
var checks = document.getElementsByName("XXX");
for(var i in checks){
if(checks[i].checked==true) counter++;
}
if(counter<10) {alert(XXX),return false;}
function checkbox(){
var checkbox=document.getElementsByName("id");
var count=0
for(var i=0;i<checkbox.length;i=i+1){
if(checkbox[i].checked){
count=count+1;
}
}
if(count<10)
alert("你选择的小于10个");
else
alert("你选择了"+count+"个");
}
</script><body>
<input type=checkbox name="id">aa
<input type=checkbox name="id">bb
<input type=checkbox name="id">cc
<input type=checkbox name="id">dd
<input type=checkbox name="id">ee
<input type=checkbox name="id">ff
<input type=checkbox name="id">gg
<input type=checkbox name="id">hh
<input type=checkbox name="id">ii
<input type=checkbox name="id">jj
<input type=checkbox name="id">bb
<input type=checkbox name="id">cc
<input type=checkbox name="id">dd
<input type=checkbox name="id">ee
<input type=checkbox name="id">ff
<input type=checkbox name="id">gg
<input type=checkbox name="id">hh
<input type=checkbox name="id">ii
<input type=checkbox name="id">jj
<input type=checkbox name="id">bb
<input type=checkbox name="id">cc
<input type=checkbox name="id">dd
<input type=checkbox name="id">ee
<input type=checkbox name="id">ff
<input type=checkbox name="id">gg
<input type=checkbox name="id">hh
<input type=checkbox name="id">ii
<input type=checkbox name="id">jj
<input type="button" value="提交" onclick="checkbox();">
</body>
大家只是提个实现方法。具体要看你的HTML页面是怎么排版的。根据不同的方法来获取到集合,接着判断!
<FORM id=v_form1 method=post name=v_form1 action=/vote.asp?a=votes><INPUT id=vid value=7 type=hidden
name=vid><!--投票名称-->
<!--投票名称结束-->
<TABLE border=0 cellSpacing=0 cellPadding=0>
<TBODY>
<TR>
<TD class=vote_jgheight></TD></TR></TBODY></TABLE>
<TABLE class=vote_border border=0 cellSpacing=0 cellPadding=0 width="980"><!--标题名称-->
<TBODY><!--标题名称结束--><!--投票选项-->
<TR>
<TD class=vote_item_bg><table width="970" border=0 class=aabbcc>
<tbody>
<tr>
<td width="25%"><IMG border=0 align=absMiddle src="/vote/7/2010062814421451952.jpg"
width=190 height=260> <BR><A
href="/viewthread.php?tid=3822"
target=_blank>情融社区 爱洒居民</A><BR>
<INPUT id=v__id__8
onclick="return CheckVote(this,8,10);" value=154 type=checkbox
name=v__id__8></td>
<td width="25%"><IMG border=0 align=absMiddle src="/vote/7/2010062814453236803.jpg"
width=190 height=260> <BR><A
href="/redirect.php?goto=findpost&ptid=3822&pid=6767"
target=_blank>真诚服务 青春飞扬</A><BR>
<INPUT id=v__id__8
onclick="return CheckVote(this,8,10);" value=155 type=checkbox
name=v__id__8></td>
<td width="25%"><IMG border=0 align=absMiddle src="/vote/7/2010062814462446828.jpg"
width=190 height=260> <BR><A
href="/redirect.php?goto=findpost&ptid=3822&pid=6768"
target=_blank>置身蜗居 心容社区</A><BR>
<INPUT id=v__id__8
onclick="return CheckVote(this,8,10);" value=156 type=checkbox
name=v__id__8></td>
<td width="25%"><IMG border=0 align=absMiddle src="/vote/7/2010062814473656355.jpg"
width=190 height=260> <BR><A
href="/redirect.php?goto=findpost&ptid=3822&pid=6769"
target=_blank>为民办事 不遗余力</A><BR>
<INPUT id=v__id__8
onclick="return CheckVote(this,8,10);" value=157 type=checkbox
name=v__id__8></td>
</tr>
<tr>
<td><img border=0 align=absMiddle src="/vote/7/2010062815184254628.jpg"
width=190 height=260 /> <br />
<a
href="/redirect.php?goto=findpost&ptid=3822&pid=6794"
target=_blank>劳动模范 埋头苦干</a><br />
<input id=v__id__8
onclick="return CheckVote(this,8,10);" value=182 type=checkbox
name=v__id__8 /></td>
<td><img border=0 align=absMiddle src="/vote/7/2010062815194424674.jpg"
width=190 height=260 /> <br />
<a
href="/redirect.php?goto=findpost&ptid=3822&pid=6795"
target=_blank>低调做人 高调做事</a><br />
<input id=v__id__8
onclick="return CheckVote(this,8,10);" value=183 type=checkbox
name=v__id__8 /></td>
<input id=item___id value=8 type=hidden name=item___id />
<input
id=item___cbox value=10 type=hidden name=item___cbox />
<input id=item___cboxfactor
value=">" type=hidden name=item___cboxfactor />
</tr>
</tbody>
</table></TD></TR><!--投票选项结束--></TBODY></TABLE>
<TABLE border=0 cellSpacing=0 cellPadding=0>
<TBODY>
<TR>
<TD height=5></TD></TR></TBODY></TABLE>
<TABLE border=0 width="980">
<TBODY>
<TR>
<TD align=middle><INPUT id=submit1 value=投票 type=submit name=submit1 > <INPUT id=button1 onclick="window.open('/show.asp?a=vote&vid=7');" value=查看 type=button name=button1>
</TD></TR></TBODY></TABLE></FORM>帮我加下吧 刚试了 还是不会弄!!
var ckblist = document.getElementByName("v__id__8");
var count = 0;
for(var i = 0;i<ckblist.length;i++)
{
if(ckblist.checked)
{
count++;
}
}if(count <10)
{
alert("请输入!")
}
var count = 0;
for(var i = 0;i<ckblist.length;i++)
{
if(ckblist.checked)
{
count++;
}
}if(count <10)
{
alert("请输入!")
}
function checkbox(){var ckblist = document.getElementsByName("v__id__8");
var count = 0;
for(var i = 0;i<ckblist.length;i++)
{
if(ckblist.checked)
{
count++;
}
}if(count <10)
{
alert("您至少选择10条!")
}
}
</script>我在 form里面加入鼠标事件 onclick="checkbox();" JS提示了 但是还是能提交
if(count <10)
{
alert("您至少选择10条!")
return;
}
function checkbox11(){
var ckblist = document.getElementsByName("v__id__8");
var count = 0;
for(var i = 0;i<ckblist.length;i++)
{
if(ckblist.checked)
{
count++;
}
}if(count <10)
{
alert("您至少选择10条!")
return;}
}
</script>还是一样不行 晕呼呼了。!!
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>checkbox.html</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8"> <script type="text/javascript">
function checkNum() {
var box = document.getElementsByName("v__id__8");
var count = 0;
for (var i = 0; i < box.length; i++) {
if (box[i].checked) {
count ++;
}
}
if (count < 10) {
alert("你选择的数量小于10个!");
}
}
</script>
</head> <body>
<FORM id=v_form1 method=post name=v_form1 action=/vote.asp?a=votes>
<INPUT id=vid value=7 type=hidden name=vid>
<!--投票名称-->
<!--投票名称结束-->
<TABLE border=0 cellSpacing=0 cellPadding=0>
<TBODY>
<TR>
<TD class=vote_jgheight></TD>
</TR>
</TBODY>
</TABLE>
<TABLE class=vote_border border=0 cellSpacing=0 cellPadding=0
width="980">
<!--标题名称-->
<TBODY>
<!--标题名称结束-->
<!--投票选项-->
<TR>
<TD class=vote_item_bg>
<table width="970" border=0 class=aabbcc>
<tbody>
<tr>
<td width="25%">
<IMG border=0 align=absMiddle
src="/vote/7/2010062814421451952.jpg" width=190 height=260>
<BR>
<A href="/viewthread.php?tid=3822" target=_blank>情融社区
爱洒居民</A>
<BR>
<INPUT id=v__id__8 onclick="return CheckVote(this,8,10);"
value=154 type=checkbox name=v__id__8>
</td>
<td width="25%">
<IMG border=0 align=absMiddle
src="/vote/7/2010062814453236803.jpg" width=190 height=260>
<BR>
<A
href="/redirect.php?goto=findpost&ptid=3822&pid=6767"
target=_blank>真诚服务 青春飞扬</A>
<BR>
<INPUT id=v__id__8 onclick="return CheckVote(this,8,10);"
value=155 type=checkbox name=v__id__8>
</td>
<td width="25%">
<IMG border=0 align=absMiddle
src="/vote/7/2010062814462446828.jpg" width=190 height=260>
<BR>
<A
href="/redirect.php?goto=findpost&ptid=3822&pid=6768"
target=_blank>置身蜗居 心容社区</A>
<BR>
<INPUT id=v__id__8 onclick="return CheckVote(this,8,10);"
value=156 type=checkbox name=v__id__8>
</td>
<td width="25%">
<IMG border=0 align=absMiddle
src="/vote/7/2010062814473656355.jpg" width=190 height=260>
<BR>
<A
href="/redirect.php?goto=findpost&ptid=3822&pid=6769"
target=_blank>为民办事 不遗余力</A>
<BR>
<INPUT id=v__id__8 onclick="return CheckVote(this,8,10);"
value=157 type=checkbox name=v__id__8>
</td>
</tr>
<tr>
<td>
<img border=0 align=absMiddle
src="/vote/7/2010062815184254628.jpg" width=190 height=260 />
<br />
<a
href="/redirect.php?goto=findpost&ptid=3822&pid=6794"
target=_blank>劳动模范 埋头苦干</a>
<br />
<input id=v__id__8 onclick="return CheckVote(this,8,10);"
value=182 type=checkbox name=v__id__8 />
</td>
<td>
<img border=0 align=absMiddle
src="/vote/7/2010062815194424674.jpg" width=190 height=260 />
<br />
<a
href="/redirect.php?goto=findpost&ptid=3822&pid=6795"
target=_blank>低调做人 高调做事</a>
<br />
<input id=v__id__8 onclick="return CheckVote(this,8,10);"
value=183 type=checkbox name=v__id__8 />
</td>
<input id=item___id value=8 type=hidden name=item___id />
<input id=item___cbox value=10 type=hidden name=item___cbox />
<input id=item___cboxfactor value=">" type=hidden
name=item___cboxfactor />
</tr>
</tbody>
</table>
</TD>
</TR>
<!--投票选项结束-->
</TBODY>
</TABLE>
<TABLE border=0 cellSpacing=0 cellPadding=0>
<TBODY>
<TR>
<TD height=5></TD>
</TR>
</TBODY>
</TABLE>
<TABLE border=0 width="980">
<TBODY>
<TR>
<TD align=middle>
<INPUT value=你选了10个没 type="button" onclick="checkNum()">
<INPUT id=submit1 value=投票 type=submit name=submit1 onclick="checkNum()">
<INPUT id=button1
onclick="window.open('/show.asp?a=vote&vid=7');" value=查看
type=button name=button1>
</TD>
</TR>
</TBODY>
</TABLE>
</FORM> </body>
</html>
if (count < 10) {
alert("你选择的数量小于10个!");
return false;
}<INPUT value=你选了10个没 type="button" onclick="return checkNum()">
alert($('input[type=checkbox][checked=true]').size()); //显示你选择了几个