微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – 如何将用户输入保存到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>

这是javascript部分:

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

解决方法

它不起作用,因为name是JavaScript中的保留字.将函数名称更改为其他名称.

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);
}

原文地址:https://www.jb51.cc/js/155203.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐