自己測試過,目前沒發現邏輯存在問題。如有問題,請多多指教必定改正。
該功能主要是項目需要,假如有指定數量輪次比賽,人爲將結果一次性提交,但提交之前必須先驗證,如一場比賽有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>