提示后javascript显示用户输入 [英] javascript displaying user input after a prompt

查看:42
本文介绍了提示后javascript显示用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示用户输入完信息后输入的内容.当用户到达最后一个提示时,我希望页面显示他们输入的内容.例如:如果他们在名称部分下输入了apples,我希望它在代码底部的P1部分列出apples一词.我是javascript的新手,并且我事先知道我的代码不稳定,但是我正在学习,所以请放轻松,请帮助我弄清楚在javascript完成后如何显示用户输入.

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

我的代码:

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

推荐答案

要在Javascript中显示变量的值,您需要将其作为DOM树中的节点之一添加到DOM(文档对象模型)中.这是一个简单的示例,供您玩耍和学习(下).

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. Here is a simple example to play around with and learn (below).

重要提示:请检查HTML文档的结构也应正确.祝你好运.

IMPORTANT: please check how the HTML document should be properly structured as well. 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> 

这篇关于提示后javascript显示用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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