<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
function getAllByName(){
var a={};
var eles=document.getElementsByTagName("input");
for(var i=0,len=eles.length;i<len;i++){
if(eles[i].type=='radio'||eles[i].type=='checkbox'){
var name=eles[i].getAttribute('name');
if(!a[name]){
a[name]=[];
}
a[name].push(eles[i]);
}
}
return a;
}
function check(){
var a=getAllByName();
for(var i in a){
if(a.hasOwnProperty(i)){
var k=a[i];
var b=false;
for(var j=0,len=k.length;j<len;j++){
if(k[j].checked){
b=true;
}
}
if(!b){
var q=a[i][0].previousSibling;
if(!q.tagName){
q=q.previousSibling;
}
alert(q.innerHTML+"未选择");
return false;
}
}
}
}
</script>
</head><body>
<form onsubmit="return check()">
<p>单选题1</p>
<input name="Q1" type="radio" value="0">是<br/>
<input name="Q1" type="radio" value="2">否<br/><p>多选题1</p>
<input name="Q2" type="checkbox" value="0">1<br/>
<input name="Q2" type="checkbox" value="1">2<br/>
<input name="Q2" type="checkbox" value="2">3<br/><p>单选题2</p>
<input name="Q3" type="radio" value="0">是<br/>
<input name="Q3" type="radio" value="2">否<br/>
<p>多选题2</p>
<input name="Q4" type="checkbox" value="0">1<br/>
<input name="Q4" type="checkbox" value="1">2<br/>
<input name="Q4" type="checkbox" value="2">3<br/><p>单选题3</p>
<input name="Q5" type="radio" value="0">是<br/>
<input name="Q5" type="radio" value="2">否<br/>
<p>多选题3</p>
<input name="Q6" type="checkbox" value="0">1<br/>
<input name="Q6" type="checkbox" value="1">2<br/>
<input name="Q6" type="checkbox" value="2">3<br/><input type="submit" value="提交" name="Submit">
</form>
</body>
</html>
类似这样试试
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
function getAllByName(){
var a={};
var eles=document.getElementsByTagName("input");
for(var i=0,len=eles.length;i<len;i++){
if(eles[i].type=='radio'||eles[i].type=='checkbox'){
var name=eles[i].getAttribute('name');
if(!a[name]){
a[name]=[];
}
a[name].push(eles[i]);
}
}
return a;
}
function check(){
var a=getAllByName();
for(var i in a){
if(a.hasOwnProperty(i)){
var k=a[i];
var b=false;
for(var j=0,len=k.length;j<len;j++){
if(k[j].checked){
b=true;
}
}
if(!b){
var q=a[i][0].previousSibling;
if(!q.tagName){
q=q.previousSibling;
}
alert(q.innerHTML+"未选择");
return false;
}
}
}
}
</script>
</head><body>
<form onsubmit="return check()">
<p>单选题1</p>
<input name="Q1" type="radio" value="0">是<br/>
<input name="Q1" type="radio" value="2">否<br/><p>多选题1</p>
<input name="Q2" type="checkbox" value="0">1<br/>
<input name="Q2" type="checkbox" value="1">2<br/>
<input name="Q2" type="checkbox" value="2">3<br/><p>单选题2</p>
<input name="Q3" type="radio" value="0">是<br/>
<input name="Q3" type="radio" value="2">否<br/>
<p>多选题2</p>
<input name="Q4" type="checkbox" value="0">1<br/>
<input name="Q4" type="checkbox" value="1">2<br/>
<input name="Q4" type="checkbox" value="2">3<br/><p>单选题3</p>
<input name="Q5" type="radio" value="0">是<br/>
<input name="Q5" type="radio" value="2">否<br/>
<p>多选题3</p>
<input name="Q6" type="checkbox" value="0">1<br/>
<input name="Q6" type="checkbox" value="1">2<br/>
<input name="Q6" type="checkbox" value="2">3<br/><input type="submit" value="提交" name="Submit">
</form>
</body>
</html>
类似这样试试
<p>单选题1</p>
<input name="Q1" type="radio" value="0">是<br/>
<input name="Q1" type="radio" value="2">否<br/><p>多选题1</p>
<input name="Q2" type="checkbox" value="0">1<br/>
<input name="Q2" type="checkbox" value="1">2<br/>
<input name="Q2" type="checkbox" value="2">3<br/><p>单选题2</p>
<input name="Q3" type="radio" value="0">是<br/>
<input name="Q3" type="radio" value="2">否<br/>
<p>多选题2</p>
<input name="Q4" type="checkbox" value="0">1<br/>
<input name="Q4" type="checkbox" value="1">2<br/>
<input name="Q4" type="checkbox" value="2">3<br/><p>单选题3</p>
<input name="Q5" type="radio" value="0">是<br/>
<input name="Q5" type="radio" value="2">否<br/>
<p>多选题3</p>
<input name="Q6" type="checkbox" value="0">1<br/>
<input name="Q6" type="checkbox" value="1">2<br/>
<input name="Q6" type="checkbox" value="2">3<br/><input type="submit" value="提交" name="Submit">
</form>
<script type="text/javascript">
function check(obj){
var inputs = obj.getElementsByTagName("input"),name="",ck,should,first;
for(var i= 0,l=inputs.length;i<l;i++){
if(inputs[i].name!=name){
if(i>0&&ck<should){
alert("请选择"+["单","多"][should-1]+"选题!");
first.focus();
return false;
}
ck = 0;
first = inputs[i];
should = first.type=="radio" ? 1 : 2;
name=first.name;
}
inputs[i].checked && ck++;
}
}
</script>
<form action="?" id="form" method="post" onsubmit="return check(this)">
<p>单选题1</p>
<input name="Q1" type="radio" value="0">是<br/>
<input name="Q1" type="radio" value="2">否<br/>
<p>多选题1</p>
<input name="Q2" type="checkbox" value="0">1<br/>
<input name="Q2" type="checkbox" value="1">2<br/>
<input name="Q2" type="checkbox" value="2">3<br/>
<p>单选题2</p>
<input name="Q3" type="radio" value="0">是<br/>
<input name="Q3" type="radio" value="2">否<br/>
<p>多选题2</p>
<input name="Q4" type="checkbox" value="0">1<br/>
<input name="Q4" type="checkbox" value="1">2<br/>
<input name="Q4" type="checkbox" value="2">3<br/>
<p>单选题3</p>
<input name="Q5" type="radio" value="0">是<br/>
<input name="Q5" type="radio" value="2">否<br/>
<p>多选题3</p>
<input name="Q6" type="checkbox" value="0">1<br/>
<input name="Q6" type="checkbox" value="1">2<br/>
<input name="Q6" type="checkbox" value="2">3<br/>
<input type="submit" value="提交" name="Submit">
</form>
<script type="text/javascript">
function CheckOK(objs) {
for (var i = 0; i < objs.length; i++) if (objs[i].checked) return true;
return false;
}
function check(obj) {
var inputs = obj.getElementsByTagName("input"), names = ",", name;
for (var i = 0; i < inputs.length; i++) {
name = inputs[i].name;
if (names.indexOf(',' + name + ',') == -1) {
if (!CheckOK(obj[name])) {
alert("请选择" + ([inputs[i].type == 'radio' ? '单' : '多']) + "选题!");
inputs[i].focus();
return false;
} names += inputs[i].name + ',';
}
}
}
</script>
for (var i = 0; i < objs.length; i++) if (objs[i].checked) return true;
return false;
}
function check(obj) {
var inputs = obj.getElementsByTagName("input"), names = ",", name;
for (var i = 0; i < inputs.length; i++) {
if (inputs[i].type != 'radio' && inputs[i].type != 'checkbox') continue;
name = inputs[i].name;
if (names.indexOf(',' + name + ',') == -1) {
if (!CheckOK(obj[name])) {
alert("请选择" + ([inputs[i].type == 'radio' ? '单' : '多']) + "选题!");
inputs[i].focus();
return false;
} names += inputs[i].name + ',';
}
}
}