如何在 JavaScript/HTML 中显示用户通过文本框输入的文本
我目前的 JavaScript 代码中有这样的内容:
<html>
<head>
<title>
Form
</title>
<script type="text/javascript">
var input = document.getElementById('textbox');
function namet()
{
alert(textbox.value);
}
var input2 = document.getElementById('location');
function namet2()
{
alert(location.value);
}
</script>
</head>
<body>
Your name:
<input type="text" name="FirstName" id="textbox" <br><br/>
Your Address:
<input type="text" name="address" id="location" <br></br>
click:
<input type="button" value="submit" onclick="namet()"/>
</body>
</html>
...并且正在寻找一种方法来收集用户在文本框中键入的内容,并在用户单击“单击”按钮时将其显示在单个警报框中,有点像确认。
此外,了解在单击警报框上的“确定”后如何将它们重定向到不同的页面也很有帮助。
谢谢。
I currently have this within my JavaScript code:
<html>
<head>
<title>
Form
</title>
<script type="text/javascript">
var input = document.getElementById('textbox');
function namet()
{
alert(textbox.value);
}
var input2 = document.getElementById('location');
function namet2()
{
alert(location.value);
}
</script>
</head>
<body>
Your name:
<input type="text" name="FirstName" id="textbox" <br><br/>
Your Address:
<input type="text" name="address" id="location" <br></br>
click:
<input type="button" value="submit" onclick="namet()"/>
</body>
</html>
...and was looking for a way to gather what the user types within the text box and shows them as in a single alert box when they click on the "Click" button, sort of like a confirmation.
Additionally it would also be helpful to find out how to redirect them to a different page after clicking OK on the alert box.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这很容易完成,而且您几乎已经完成了!只是一些命名错误的变量...
根据您给出的示例,将:更改
为:
希望有帮助。
--根据注释进行编辑--
将两个单独的函数合而为一,然后继续在函数内设置变量,如下所示:
--re-edit:像个白痴一样,忘记了 .value... 已修复。
This is easily accomplished, and you almost had it! Just some misnamed variables...
Given the example you gave, change:
to:
Hope that helps.
--edit based on comment--
Turn the two individual functions into one, and go ahead and set the variables inside the function, as such:
--re-edit: Like an idiot, forgot the .value... fixed.
请使用下面的代码:
Please use below code:
使用 jquery
和表单
希望这有帮助
Use jquery
and the form
Hope this helps