如何将用户输入保存到html和js中的变量中 [英] How to save user input into a variable in html and js

查看:487
本文介绍了如何将用户输入保存到html和js中的变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一款游戏,并在开始时询问你的名字。我希望这个名字被保存为可变的。我有这个html代码:

 < form id =formonsubmit =return false;> 
< input style = position:absolute; top:80%; left:5%; width:40%; type =textid =userInput>
< input style = position:absolute; top:50%; left:5%; width:40%; type =submitonclick =name()>
< / form>

,这是javascript部分:

 function name()
{
var input = document.getElementById(userInput);
alert(输入);
}


解决方案

name 是JavaScript中的保留字。将函数名改为别的。



请参阅 http://www.quackit.com/javascript/javascript_reserved_words.cfm

 < form id = formonsubmit =return false;> 
< input style =position:absolute; top:80%; left:5%; width:40%; type =textid =userInput/>
< input style =position:absolute; top:50%; left:5%; width:40%; type =submitonclick =othername(); />
< / form>

函数othername(){
var input = document.getElementById(userInput)。value;
alert(输入);
}


i am making a game and at the start it asks for your name. I want this name to be saved as varible. i have this html code:

<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>

and this is javascript part:

function name()
{
var input = document.getElementById("userInput");
alert(input);
}

解决方案

It doesn't work because name is a reserved word in JavaScript. Change the function name to something else.

See http://www.quackit.com/javascript/javascript_reserved_words.cfm

<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="othername();" />
</form>

function othername() {
    var input = document.getElementById("userInput").value;
    alert(input);
}

这篇关于如何将用户输入保存到html和js中的变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆