的$.inArray
总是返回真为我的用户群检查jQuery的inArray总是返回true
具体的代码是在run
功能和checkUserCondition
功能。
如果您运行该程序,它将显示带有计数的消息。
点击“输入自旋”,然后点击“切换数字小”再加入8号
用户组组1,2,3,4,5和组5,6,7注册为8, 8
为什么$.inArray()
的行为如此?
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>
<head>
<style>
.bntcal {
padding: 10px 10px 10px 10px;
\t margin: 10px;
}
.bntcaladd {
padding: 10px 10px 10px 10px;
\t margin: 10px;
}
.bntshow{
padding: 5px 5px 5px 5px;
\t margin: 5px;
\t width: 10%;
}
.spanhide {
\t display: none;
}
.bell {
\t float:left;
\t display: none;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
var wheel0 = [37,32,15,19,4,21,2,25,17,34,6,27,13,36,11,30,8,23,10,5,24,16,33,1,20,14,31,9,22,18,29,7,28,12,35,3,26];
var wheel00 = [37,28,9,26,30,11,7,20,32,17,5,22,34,15,3,24,36,13,1,38,27,10,25,29,12,8,19,31,18,6,21,33,16,4,23,35,14,2];
var wheel = wheel00;
var wColor = "";
var numbers = [];
var color = [];
$(document).ready(function(){
\t // --- Menu Buttons
$("#enterSpins").click(function(){
\t \t $("#divSpins").show();
\t \t $("#divAlerts").hide();
$("#divHowTo").hide();
});
\t
\t $("#setAlert").click(function(){
\t \t $("#divAlerts").show();
\t \t $("#divSpins").hide();
$("#divHowTo").hide();
});
\t
\t $("#howTo").click(function(){
\t \t $("#divHowTo").show();
\t \t $("#divSpins").hide();
$("#divAlerts").hide();
\t
\t });
\t
\t // ---- Sub Buttons
\t $("#enterPad").click(function(){
\t \t $("#showSpinsPad").toggle();
\t \t $("#clearBox").toggle();
\t \t $("#clearBox1").toggle();
\t \t if(!$("#showSpinsPad").is(":hidden"))
\t \t {
\t \t \t $("#spins").prop("disabled", true)
\t \t }
\t \t else
\t \t {
\t \t \t $("#spins").prop("disabled", false)
\t \t }
\t \t
}); \t
\t
\t $(".bntcal").click(function(){
\t \t $("#currentSpin").text($("#currentSpin").text()+$(this).val())
\t \t if ($("#currentSpin").text().length > 2)
\t \t {
\t \t \t $("#currentSpin").text($(this).val())
\t \t }
\t });
\t \t
\t
\t $("#clear").click(function(){
\t \t $("#currentSpin").text("")
});
\t
\t $("#add").click(function(){
\t
\t \t getWheel()
\t \t currentSpin = $("#currentSpin").text()
\t \t if(currentSpin.length > 0)
\t \t {
\t \t \t if ($("#spins").val().length > 0)
\t \t \t {
\t \t \t \t $("#spins").val($("#spins").val() + ", ");
\t \t \t }
\t \t \t if(parseInt(currentSpin)<37)
\t \t \t {
\t \t \t \t $("#spins").val($("#spins").val() + currentSpin);
\t \t \t }
\t \t \t if(parseInt(currentSpin) == 37)
\t \t \t {
\t \t \t \t $("#spins").val($("#spins").val() + "0");
\t \t \t }
\t \t \t if(parseInt(currentSpin) == 38)
\t \t \t {
\t \t \t \t $("#spins").val($("#spins").val() + "00");
\t \t \t }
\t \t \t if(parseInt(currentSpin)>38)
\t \t \t {
\t \t \t \t $("#spins").val($("#spins").val() + "0");
\t \t \t }
\t \t \t $("#currentSpin").text("")
\t \t }
\t \t
\t \t var n = GetUserNumbers();
\t \t run(n)
});
\t
\t $("#undo").click(function(){
\t \t var n = GetUserNumbers();
\t \t
\t \t if (n.length>0)
\t \t {
\t \t \t n.pop();
\t \t }
\t \t $("#spins").val(n)
\t \t run(n)
});
\t
\t $("#clean").click(function(){
\t \t $("#spins").val("")
});
\t
\t $("#run").click(function(){
\t \t var n = GetUserNumbers();
\t \t run(n)
});
\t
});
// ---------------------------------------------------------------
// --------------------- FUNCTIONS -------------------------------
// ---------------------------------------------------------------
function run(n)
{
\t
\t $("#alert").text("")
\t // Evens
\t var conditions = ['color[n] == "red"','color[n] == "black"','n%2 == 0 && n<37','n%2 == 1 && n<37','n > 18 && n<37','n < 19 && n>0']
\t var checklocation = ['Red','Black','Even','Odd','High','Low']
\t var idWins = "#evenwin"
\t var idLosses = "#evenloss"
\t checkCondition(n, conditions, checklocation, idWins, idLosses)
\t // Dozens
\t var conditions = ['n%3 == 1 && n<37','n%3 == 2 && n<37','n%3 == 0 && n<37','n>=1 && n<=12','n>=13 && n<=24','n>=25 && n<=36']
\t var checklocation = ['Row 1 [1,2,3..]','Row 2 [2,3,4..]','Row 2 [3,6,9..]','Dozen 1 [1-12]','Dozen 1 [13-24]','Dozen 1 [25-36]']
\t var idWins = "#dozenwin"
\t var idLosses = "#dozenloss"
\t checkCondition(n, conditions, checklocation, idWins, idLosses)
\t
\t // User Groups
\t var groups = $("#usergroup1").val().split('-')
\t var userNumbers = []
\t for(var i=0; i<groups.length; i++)
\t {
\t \t userNumbers.push(groups[i].split(','))
\t }
\t var idWins = "#userwin1"
\t var idLosses = "#userloss1"
\t checkUserCondition(n, checklocation, idWins, idLosses, userNumbers)
\t
}
function checkUserCondition(n, checklocation, idWins, idLosses, arr)
{
\t var check = function(n, a){ if($.inArray(n, a)){ return true; } else { return false; }}
\t
\t for(var ia=0; ia<arr.length; ia++)
\t {
\t \t var wins = parseInt(0)
\t \t var losses = parseInt(0)
\t \t wins = checkUserWins(n, check,arr[ia])
\t \t losses = checkUserLosses(n, check,arr[ia])
\t \t alert(arr[ia])
\t \t $("#alert").append("note User Group: "+arr[ia]+" Win count: "+wins+"<br/>")
\t \t //if(wins >= $(idWins).val()) { $("#alert").append(checklocation[i]+" loses "+wins+" times in a rows.<br/>")}
\t \t //if(losses >= $(idLosses).val()) { $("#alert").append(checklocation[i]+" loses "+losses+" times in a rows.<br/>")}
\t }
\t
}
function checkCondition(n, conditions, checklocation, idWins, idLosses)
{
\t for(var i=0; i<conditions.length; i++)
\t {
\t \t var wins = parseInt(0)
\t \t var losses = parseInt(0)
\t \t var check = function(n){ if(eval(conditions[i])){ return true; } else { return false; }}
\t \t wins = checkWins(n, check)
\t \t
\t \t $("#alert").append("note: "+checklocation[i]+" Condition: "+conditions[i]+" Win count: "+wins+"<br/>")
\t \t
\t \t losses = checkLosses(n, check)
\t \t //if(wins >= $(idWins).val()) { $("#alert").append(checklocation[i]+" loses "+wins+" times in a rows.<br/>")}
\t \t //if(losses >= $(idLosses).val()) { $("#alert").append(checklocation[i]+" loses "+losses+" times in a rows.<br/>")}
\t }
\t
}
function checkUserWins(n, condition, arr)
{
\t var wins = parseInt(0);
\t for(var i=0;i<n.length;i++)
\t {
\t \t if(condition(parseInt(n[i]),arr))
\t \t {
\t \t \t wins += 1
\t \t }
\t \t else
\t \t {
\t \t \t wins = 0
\t \t } \t
\t }
\t
\t return wins \t
}
function checkUserLosses(n, condition, arr)
{
\t var losses = parseInt(0);
\t for(var i=0;i<n.length;i++)
\t {
\t \t if(condition(n[i],arr))
\t \t {
\t \t \t losses = 0
\t \t }
\t \t else
\t \t {
\t \t \t losses += 1
\t \t }
\t }
\t return losses
}
function checkWins(n, condition)
{
\t var wins = parseInt(0);
\t for(var i=0;i<n.length;i++)
\t {
\t \t if(condition(n[i]))
\t \t {
\t \t \t wins += 1
\t \t }
\t \t else
\t \t {
\t \t \t wins = 0
\t \t } \t
\t }
\t
\t return wins \t
}
function checkLosses(n, condition)
{
\t var losses = parseInt(0);
\t for(var i=0;i<n.length;i++)
\t {
\t \t if(condition(n[i]))
\t \t {
\t \t \t losses = 0
\t \t }
\t \t else
\t \t {
\t \t \t losses += 1
\t \t }
\t }
\t return losses
}
$.fn.multiline = function(text){
this.text(text);
this.html(this.html().replace(/\n/g,'<br/>'));
return this;
}
function getWheel()
{
\t if($("input[name=wheel]:checked").val()=="0")
\t {
\t \t wheel = wheel0
\t }
\t else
\t {
\t \t wheel = wheel00
\t }
\t color = GetColor(wheel)
}
function reset()
{
\t evenwins = parseInt(0);
}
function GetUserNumbers()
{
\t var n = $("#spins").val().split(",");
\t
\t for(var x=0; x<n.length; x++)
\t {
\t \t if(n[x].trim() == "0")
\t \t {
\t \t \t n[x] = "37";
\t \t }
\t \t if(n[x].trim() == "00")
\t \t {
\t \t \t n[x] = "38";
\t \t }
\t \t n[x] = parseInt(n[x].trim());
\t }
\t return n;
}
function GetColor(wheel)
{
\t var color = Array();
\t if(wheel.length==37)
\t {
\t \t color[1] = "red";
\t \t color[2] = "black";
\t \t color[3] = "red";
\t \t color[4] = "black";
\t \t color[5] = "red";
\t \t color[6] = "black";
\t \t color[7] = "red";
\t \t color[8] = "black";
\t \t color[9] = "red";
\t \t color[10] = "black";
\t \t color[11] = "black";
\t \t color[12] = "red";
\t \t color[13] = "black";
\t \t color[14] = "red";
\t \t color[15] = "black";
\t \t color[16] = "red";
\t \t color[17] = "black";
\t \t color[18] = "red";
\t \t color[19] = "red";
\t \t color[20] = "black";
\t \t color[21] = "red";
\t \t color[22] = "black";
\t \t color[23] = "red";
\t \t color[24] = "black";
\t \t color[25] = "red";
\t \t color[26] = "black";
\t \t color[27] = "red";
\t \t color[28] = "black";
\t \t color[29] = "black";
\t \t color[30] = "red";
\t \t color[31] = "black";
\t \t color[32] = "red";
\t \t color[33] = "black";
\t \t color[34] = "red";
\t \t color[35] = "black";
\t \t color[36] = "red";
\t \t color[37] = "green";
\t }
\t if(wheel.length==38)
\t {
\t \t color[1] = "red";
\t \t color[2] = "black";
\t \t color[3] = "red";
\t \t color[4] = "black";
\t \t color[5] = "red";
\t \t color[6] = "black";
\t \t color[7] = "red";
\t \t color[8] = "black";
\t \t color[9] = "red";
\t \t color[10] = "black";
\t \t color[11] = "black";
\t \t color[12] = "red";
\t \t color[13] = "black";
\t \t color[14] = "red";
\t \t color[15] = "black";
\t \t color[16] = "red";
\t \t color[17] = "black";
\t \t color[18] = "red";
\t \t color[19] = "red";
\t \t color[20] = "black";
\t \t color[21] = "red";
\t \t color[22] = "black";
\t \t color[23] = "red";
\t \t color[24] = "black";
\t \t color[25] = "red";
\t \t color[26] = "black";
\t \t color[27] = "red";
\t \t color[28] = "black";
\t \t color[29] = "black";
\t \t color[30] = "red";
\t \t color[31] = "black";
\t \t color[32] = "red";
\t \t color[33] = "black";
\t \t color[34] = "red";
\t \t color[35] = "black";
\t \t color[36] = "red";
\t \t color[37] = "green";
\t \t color[38] = "green";
\t }
\t return color;
}
</script>
</head>
<body>
Toggle Displays</br><br/>
<div >
<button class="bntshow" id="enterSpins">Enter Spins</button><br/>
<button class="bntshow" id="setAlert">Set Alerts</button><br/>
<button class="bntshow" id="howTo">How To</button><br/>
<input type="radio" name="wheel" value="0">0 Wheel<br/>
<input type="radio" name="wheel" value="00" checked>00 Wheel<br/>
</div><br/>
<div id="alert"></div>
<hr>
<!-- Enter Spins -->
<span class="spanhide" id="divSpins">
<button class="bntcaladd" id="enterPad">Toggle Numberpad</button><br/>
<br/>
<span class="spanhide" id='showSpinsPad'>
<br/>
<button class="bntcal" id="calculator" value="0">0</button>
<button class="bntcal" id="calculator" value="00">00</button><br/>
<button class="bntcal" id="calculator" value="1">1</button>
<button class="bntcal" id="calculator" value="2">2</button>
<button class="bntcal" id="calculator" value="3">3</button><br/>
<button class="bntcal" id="calculator" value="4">4</button>
<button class="bntcal" id="calculator" value="5">5</button>
<button class="bntcal" id="calculator" value="6">6</button><br/>
<button class="bntcal" id="calculator" value="7">7</button>
<button class="bntcal" id="calculator" value="8">8</button>
<button class="bntcal" id="calculator" value="9">9</button><br/>
<button class="bntcaladd" id="clear">Clear</button>
<button class="bntcaladd" id="add">ADD</button><span id="currentSpin"></span>
<br/>
<button class="bntcaladd" id="undo">Undo</button><br/>
<span id="currentSpin"></span>
<br/>
</span>
<span id="clearBox">Enter roulette numbers separated by a comma</span><br/>
<textarea rows="10" cols="50" id="spins"></textarea><br/><br/>
<span id="clearBox1">
<button id="clean">Clear Box</button>
<button id="run">Run</button>
</span>
</span>
<!-- Set Alerts -->
<span class="spanhide" id="divAlerts">
<h2>Evens</h2>
<input type="text" id="evenwin" value="2"> Wins in a row<br>
<input type="text" id="evenloss" value="6"> Losses in a row<br>
<br><br>
<h2>Dozens</h2>
<input type="text" id="dozenwin" value="6"> Wins in a row<br>
<input type="text" id="dozenloss" value="6"> Losses in a row<br>
<br><br>
<h2>User Group</h2>
<input type="text" id="usergroup1" value="1,2,3,4,5-5,6,7,8"> Number Group<br>
<input type="text" id="userwin1" value="1"> Wins in a row<br>
<input type="text" id="userloss1" value="6"> Losses in a row<br>
<br><br>
</span>
<!-- How To -->
<span class="spanhide" id="divHowTo">
How To
</span>
</body>
</html>
究竟是什么问题? – Chax
为什么InArray总是为我的用户组返回true? – user1854438
你可否只发布函数的部分和简单的几行代码,你实际上有问题。 –