Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/420.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在javascript中使用选择器的用户输入?_Javascript_Select - Fatal编程技术网

如何在javascript中使用选择器的用户输入?

如何在javascript中使用选择器的用户输入?,javascript,select,Javascript,Select,我跟随代码学院制作了一个石头剪刀游戏。 我想做一个选择选项,用户可以选择石头、布或剪刀。当我按下submit按钮时,我想将所选选项转移到主功能,主功能会弹出一个警报,告诉我这是平局还是胜利。但是,当我按下提交按钮时,什么都没有发生。。。这是我的密码: <h1> Rock Paper Scissors</h1> <form action="" onsubmit="return Winner()">

我跟随代码学院制作了一个石头剪刀游戏。 我想做一个选择选项,用户可以选择石头、布或剪刀。当我按下submit按钮时,我想将所选选项转移到主功能,主功能会弹出一个警报,告诉我这是平局还是胜利。但是,当我按下提交按钮时,什么都没有发生。。。这是我的密码:

<h1> Rock Paper Scissors</h1>
                        <form action="" onsubmit="return Winner()">
                                <h3> Select Your Move</h3>
                                <select id="moves">
                                    <option value="rock">Rock</option>
                                    <option value="paper">Paper</option>
                                    <option value="scissors">Scissors</option>
                                    </select>
                                <br>
                            <input type="submit" value="Submit" id="submit-button">
                        </form>

<script type="text/javascript">
function Winner(){

if (document.getElementById("moves").value = "rock"
    var userChoice = "rock";
else if (document.getElementById("moves").value = "paper"
    var userChoice = "paper";
else (document.getElementById("moves").value = "scissors"
    var userChoice = "scissors"

 var computerChoice = Math.random();
        if (computerChoice < 0.34) {
             computerChoice = "rock";
        } else if(computerChoice <= 0.67) {
            computerChoice = "paper";
        } else {
            computerChoice = "scissors";
        }

var compare = function(userChoice, computerChoice) {

    if (userChoice === computerChoice) {
        return ("Computer picked" + " " + computerChoice + "." + " " + "The result is a tie!" + "\n Refresh the page to play again!")
    }
    else if (userChoice === "rock") {
        if (computerChoice === "scissors")
        return ("Computer picked" + " " + computerChoice + "," + " " + "you win!");
        else { 
            return ("Computer picked" + " " + computerChoice + "," + " " + "you lose!")}
    }
    else if (userChoice === "paper") {
        if (computerChoice === "rock")
        return ("Computer picked" + " " + computerChoice + "," + " " + "you win!");
        else { 
            return ("Computer picked" + " " + computerChoice + "," + " " + "you lose!")}
    }
    else {
        if (computerChoice === "rock")
        return ("Computer picked" + " " + computerChoice + "," + " " + "you win!");
        else { 
            return ("Computer picked" + " " + computerChoice + "," + " " + "you lose!")}
    }  
}

alert(compare(userChoice, computerChoice));
}
        </script>
石头剪刀
选择您的移动
摇滚乐
纸张
剪刀

函数Winner(){ if(document.getElementById(“moves”).value=“rock” var userChoice=“rock”; else if(document.getElementById(“移动”).value=“纸张” var userChoice=“纸张”; else(document.getElementById(“moves”).value=“剪刀” var userChoice=“剪刀” var computerChoice=Math.random(); 如果(计算机选择<0.34){ computerChoice=“rock”;
}否则,如果(computerChoice检查JS控制台,因为Winner()函数中存在语法错误(您在其中设置了
userChoice
变量。这应该可以解决问题…

您有几个语法错误导致您的问题,请参见下面的注释:

请记住,
=
在设置值时检查相等性

函数Winner(){
如果(document.getElementById(“moves”).value==“rock”)//您在这里错误地输入了一个`=`和一个`),那么,`=`检查相等性,`=`设置一个值
var userChoice=“rock”;
else if(document.getElementById(“moves”).value==“paper”)//您在这里误用了a`=`和a`)
var userChoice=“纸张”;
else(document.getElementById(“moves”).value==“剪刀”)//您在这里误用了a`=`和a`)`
var userChoice=“scissors”//此处缺少“;”
var computerChoice=Math.random();
如果(计算机选择<0.34){
computerChoice=“rock”;

}else if(计算机选择)这可能是问题所在,也可能不是问题所在,但是您的if/else语句缺少右括号。此外,您应该使用
==
比较:
if(document.getElementById(“moves”)。value==“rock”)
在代码审查网站上发布javascript代码:PYour代码包含多个语法和逻辑错误。缺少分号,缺少),在if语句中使用=而不是==。首先修复这些问题。@gab06代码审查网站的工作是审查工作代码。@downvoter:要从我的错误中吸取教训,最好知道什么是反对票……谢谢