标签:js
自己测试过,目前没发现逻辑存在问题。如有问题,请多多指教必定改正。
该功能主要是项目需要,假如有指定数量轮次比赛,人为将结果一次性提交,但提交之前必须先验证,如一场比赛有5局3胜决定胜负,如果你提交前面两轮为空,后面三局胜利,这明显是不符合逻辑的。
请转载此文的朋友务必附带原文链接,谢谢。
原文链接:http://xuyran.blog.51cto.com/11641754/1893245
<!DOCTYPE html>
<html>
<head>
<meta charset= "utf-8">
<title>dddd</title>
<script src="jquery-1.9.1.min.js"></script>
<style>
*{
margin: 0px;
padding: 0px;
list-style: none;
}
.win-list{
width: 300px;
padding-top: 30px;
margin: auto;
}
.win-list li{
height: 25px;
}
p{
width: 15px;
height: 15px;
border: solid 1px #96E555;
float: left;
margin-left: 15px;
}
p.active{
background: red;
}
.test{
text-align: center;
}
</style>
</head>
<ul class="win-list">
<a></a>
<li><p></p><p></p></li>
<li><p></p><p></p></li>
<li><p></p><p></p></li>
<li><p></p><p></p></li>
<li><p></p><p></p></li>
</ul>
<div class="test">
<button id="test" style="margin: auto;">test</button>
<button id="reset" >重置</button>
</div>
<body>
<a>测试</a>
<script>
$(function(){
$(".win-list li p").on("click",function(){
$(this).addClass("active").siblings().removeClass("active");
})
$("#reset").on("click",function(){
$(".win-list li p").removeClass("active");
})
$("#test").on("click",function(){
var a = 0,b = 0,arr = [],arra = [],arrb = [],arrc = [];
$(".win-list li").each(function(i,v){
if($(this).find("p").eq(0).hasClass("active")){
a++;
arra.push(i);
arr.push(i);
}else if($(this).find("p").eq(1).hasClass("active")){
b++;
arrb.push(i);
arr.push(i);
}
})
if(a !== 3 && b!== 3){ //3 代表比赛赢的次数 该出判断如果胜局不等于3局,则不合法
console.log("结果不合法");
return false;
}
console.log(arr);
for(var j = 0; j < 5; j++){ //5代表比赛总局数
if(arr[j] !== j){ //如果中间有轮成绩为空,跳过,则不合法
console.log("结果不合法");
return false;
}
arrc.push(j);
}
if(arrc.toString() === arra.toString() && arrb.length !== 0){
console.log("结果不合法"); //如果a代表队前三局都胜,而b代表队后面两局还有成绩,则不合法
return false;
}
if(arrc.toString() === arrb.toString() && arra.length !== 0){
console.log("结果不合法"); //如果b代表队前三局都胜,而a代表队后面两局还有成绩,则不合法
return false;
}
console.log("结果合法");
})
})
</script>
</body>
</html>本文出自 “没有翅膀的菜鸟的进阶” 博客,请务必保留此出处http://xuyran.blog.51cto.com/11641754/1893245
标签:js
原文地址:http://xuyran.blog.51cto.com/11641754/1893245