简体   繁体   English

javascript在提示后显示用户输入

[英]javascript displaying user input after a prompt

I am wanting to display what the user entered after the user has finished inputting in information.我想在用户完成信息输入后显示用户输入的内容。 When the user gets to the last prompt I want the page to display what they entered in. For example: if they entered in apples under the name portion I want it to list the word apples in the P1 section of my code at the bottom.当用户到达最后一个提示时,我希望页面显示他们输入的内容。例如:如果他们在名称部分下输入 apples,我希望它在底部代码的 P1 部分列出单词 apples。 I am new to javascript and in advance, I know my code is choppy but I am learning so please go easy on me and please help me figure out how to display user-input after the javascript has finished.我是 javascript 的新手,并且事先,我知道我的代码是断断续续的,但我正在学习,所以请对我放轻松,请帮助我弄清楚如何在 javascript 完成后显示用户输入。

My code:我的代码:

<!DOCtype html>
<html> 
<head>
<title> This will display some answers </title> 
<body>
<script type="text/javascript">

var user = prompt("Welcome to learning about allocations with me, mr fields. 
In this tutorial we will be learning about alloction exceptions and what not 
to do with them. Let's get started, shall we? ").toUpperCase(); 

 switch(user){
    case 'YES':
     var user_1 = prompt("What's your name?"); 
     switch(user_1) {
        case 'Buster':
            prompt("Hey, brother!");
            break;
        case 'Alex':
            prompt("I've made a huge mistake.");
            break;
        case 'Steve':
            prompt("Steve Holt!");
            break;
     default:
         prompt("I don't know you!");}
break; 
default:
    prompt("too bad!"); 
}
</script>
 </body>
 </head>
 <h1> answer </h1> 
 <p1> "answers should go here" </p1> 

</html> 

 var user_1 = prompt("What's your name?"); var answerElement = document.getElementById("answer") answerElement.innerText = user_1;
 <p id="answer"></p>

To print the value of the variable in Javascript you need to add it to the DOM (document object model) as one of the nodes in the DOM tree.要在 Javascript 中打印变量的值,您需要将它作为 DOM 树中的节点之一添加到 DOM(文档对象模型)中。 Here is a simple example to play around with and learn (below).这是一个简单的例子,可以玩和学习(如下)。

IMPORTANT: please check how the HTML document should be properly structured as well.重要提示:请检查 HTML 文档的结构是否正确。 Good luck.祝你好运。

<!DOCTYPE html>
<html> 
    <head>
        <title>This will display some answers</title> 
        <script type="text/javascript">
            window.onload = function(){
                var username = prompt("What is your name?");
                var usernameElement = document.getElementById("name");
                usernameElement.innerText = username;
            }
        </script>
    </head>
    <body>
        <p id="name"></p>
    </body>
</html> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM