且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-01-14 11:36:16

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(); /&GT;
< / 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);
}