日期:2014-05-20  浏览次数:20607 次

js checkbox判断一个都没选中submit不跳转
方法一:js
  function check(){
var checked=false;
var id= document.getElementsByName("anc_who");
for(var i=0;i<id.length;i++){
if(id[i].checked){
checked=true;
}
}
if(!checked){
alert("请选择要提交的内容");
return false;
}
  }
   


  jsp
  <form action="Announcement_Add" target="InfoIframe" onsubmit="check()">
<table style="border:0px;">
<tr>
<td><input type="checkbox" name="anc_who" value="1" class="checkbox" /> 学生
<input type="checkbox" name="anc_who" value="2" class="checkbox" /> 老师
<input type="checkbox" name="anc_who" value="3" class="checkbox" /> 学生、老师
</td>
</tr>
<tr>
<td><input type="submit" value="确定" /></td>
<td><input type="reset" value="取消" /></td>
</tr>
</table>
</form>
方法二:js
  function check(){
var checked=false;
var id= document.getElementsByName("anc_who");
for(var i=0;i<id.length;i++){
if(id[i].checked){
checked=true;
}
}
if(!checked){
alert("请选择要提交的内容");
return false;
}else{
confirm ("确定要提交吗?");
var form = document.forms['form']; 
form.action="<%=request.getContextPath() %>/Announcement_Add.action";
}
  }
  jsp
  <form action="" target="InfoIframe" onsubmit="check()">
<table style="border:0px;">
<tr>
<td><input type="checkbox" name="anc_who" value="1" class="checkbox" /> 学生
<input type="checkbox" name="anc_who" value="2" class="checkbox" /> 老师
<input type="checkbox" name="anc_who" value="3" class="checkbox" /> 学生、老师
</td>
</tr>
<tr>
<td><input type="submit" value="确定" /></td>
<td><input type="reset" value="取消" /></td>
</tr>
</table>
</form>
两种方法都不可用:第一种是return false后仍然跳转;而第二种return false后则打开又打开一个页面(本页面)

------解决方案--------------------
onsubmit="return check()">

------解决方案--------------------
探讨
onsubmit="return check()">

------解决方案--------------------
<form action="" target="InfoIframe" onsubmit="return check()">