我正在制作一个游戏,一开始它会询问你的名字.我希望将此名称保存为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) 我写了这个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)