小编Ham*_*lik的帖子

如何将用户输入保存到html和js中的变量中

我正在制作一个游戏,一开始它会询问你的名字.我希望将此名称保存为varible.我有这个HTML代码:

<form id="form" onsubmit="return false;">
<input   style=position:absolute;top:80%;left:5%;width:40%; type="text" id="userInput">
<input   style=position:absolute;top:50%;left:5%;width:40%; type="submit"    onclick="name()">
</form>
Run Code Online (Sandbox Code Playgroud)

这是javascript部分:

function name()
{
var input = document.getElementById("userInput");
alert(input);
}
Run Code Online (Sandbox Code Playgroud)

html javascript user-input

12
推荐指数
2
解决办法
16万
查看次数

javascript如果功能,我做错了什么?

我写了这个javascript函数,但似乎没有用.谁能告诉我哪里出错了?它应该是正确的,但经过测试后,生成一些数字似乎有效,我认为它的if语句有问题.任何帮助,将不胜感激

<html>
    <head>
        <script>
            function myfunction()
            {
                var x=Math.floor((Math.random()*3)+1);
                myfunc();
            }

            function myfunc()
            {
                if (x == 1)
                {
                    document.write("1")
                }
                else if (x == 2)
                {
                    document.write("2")
                }
                else if (x == 3)
                {
                    document.write("3")
                }
            }
        </script>
    </head>
    <body>
        <button onclick="myfunction()">Click me</button>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

javascript if-statement

-5
推荐指数
2
解决办法
82
查看次数

标签 统计

javascript ×2

html ×1

if-statement ×1

user-input ×1