简体   繁体   English

比较属性值和随机数(JavaScript / jQuery)

[英]Comparing attribute value with a random number (JavaScript/jQuery)

I have a problem with comparing an attribute value and a random number (created in a separated function). 我在比较属性值和随机数(在单独的函数中创建)时遇到问题。 In my html I have several divs within a surrounding div. 在我的HTML中,周围的div中有几个div。 Each div within the surrounding element has an attribute, called 'value', the first one contains the value: 1, the second one: 2, ... Like this: 周围元素中的每个div都有一个名为“值”的属性,第一个包含值:1,第二个包含值:2,...像这样:

      <div id="Stage_placeholder_rectangles">
        <div id="Stage_placeholder_rectangles_red" value=0 width=25 height=25/>
        <div id="Stage_placeholder_rectangles_pink" value=1 width=25 height=25/>
        <div id="Stage_placeholder_rectangles_orange" value=2 width=25 height=25/>
        ...
      </div>

Now I want to compare the attribute 'value' with a random number, which was made to show random random colors: The function is lik this: 现在,我想将属性“值”与一个随机数进行比较,以显示随机的随机颜色:函数类似于:

function randomNumber(){
        random = Math.floor(Math.random() * 9);
        return random;
}


function randomColors(){
    while(pastRandom == newRandom){
        newRandom = randomNumber();
    }
    pastRandom = newRandom;
    switch(newRandom){
        case 0:
            stageRef.getSymbol("placeholder_colors").stop("red");
            break;
        case 1:
            stageRef.getSymbol("placeholder_colors").stop("black");
            break;
        case 2:
            stageRef.getSymbol("placeholder_colors").stop("yellow");
            break;
        case 3:
            stageRef.getSymbol("placeholder_colors").stop("green");
            break;
        case 4:
            stageRef.getSymbol("placeholder_colors").stop("orange");
            break;
        case 5:
            stageRef.getSymbol("placeholder_colors").stop("pink");
            break;
        case 6:
            stageRef.getSymbol("placeholder_colors").stop("blue");
            break;
        case 7:
            stageRef.getSymbol("placeholder_colors").stop("gray");
            break;
        case 8:
            stageRef.getSymbol("placeholder_colors").stop("purple");
            break;
    }
    checkColor(newRandom);
}

In my function checkColor, I want to compare the random number with the value that is stored in the attribute value of 'placeholder_rectagles_orange', 'placeholder_rectagles_red', ... This is my function: 在我的函数checkColor中,我想将随机数与存储在'placeholder_rectagles_orange','placeholder_rectagles_red'等属性值中的值进行比较...这是我的函数:

function checkColor(rnd){
    if(gameStarted){
        $("#Stage_placeholder_rectangles").click(function(e){
            if($("#"+e.target.id).attr("value") == rnd){
                console.log("right");
            }
            else if($("#"+e.target.id).attr("value") != rnd){
            console.log("false");
            gameOver = true;
            }
        });
    }
}

My problem is, that when I the right element (the one with the same value in his attribute 'value' as the random number, I get in my console: "right" and multiple times "false", even when my attributes value and random number are the same. 我的问题是,当我选择正确的元素时(他的属性“值”中的值与随机数相同),即使在我的属性值和随机数是相同的。

I think part of your problem is the usage of rnd , which is not defined earlier, but we don't have all of your code. 我认为您的问题的一部分是rnd的用法,该用法之前没有定义,但是我们没有所有的代码。

Here it is working: 它在这里工作:

<div>Please click the colour: <span id="name"></span></div>
<p/>
<div id="Stage_placeholder_rectangles">
    <div id="Stage_placeholder_rectangles_red" class="square red" value=0 width="25" height="25"></div>
    <div id="Stage_placeholder_rectangles_pink" class="square pink" value=1 width="25" height="25"></div>
    <div id="Stage_placeholder_rectangles_orange" class="square orange" value=2 width=25 height="25"></div>
 </div>
<p/>
<div id="result"></div>

And the JS: 和JS:

var colors = ["red","pink","orange"],
gameStarted = true, newRandom, pastRandom;

function randomNumber(){
    random = Math.floor(Math.random() * 3);
    return random;
}


var giveOption = function() {
    while(pastRandom == newRandom){
        newRandom = randomNumber();
    }
    pastRandom = newRandom;
    $("#name").html(colors[newRandom]);
};
$("#Stage_placeholder_rectangles").click(function(e){
    var val = $(e.target).attr("value"), 
        resultDiv = $("#result");
    if(val == newRandom){
        resultDiv.html("right!");
        giveOption();
    } else {
        resultDiv.html("Wrong! Sorry. Game over");
       gameOver = true;
    }
});

giveOption();

There also is a lot of stuff that could be simplified using arrays and jquery, but I left it for now. 还有很多可以使用数组和jquery简化的东西,但我现在暂时不说了。

Fully functional jsfiddle at http://jsfiddle.net/obqmwLet/1/ 功能齐全的jsfiddle,位于http://jsfiddle.net/obqmwLet/1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM