JavaScript Prompt() 方法 [英] JavaScript Prompt() method

查看:52
本文介绍了JavaScript Prompt() 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一项正在处理的作业,但我在使用 prompt() 方法时遇到了问题.我看到我可以做一个提示,但我需要几个和数量.

I have an assignment I'm working on and I am having a problem with the prompt() method. I see that I can do one prompt, but I need several and with amounts.

例如...

我创建了一个 HTML 表格,其中包含许多艺术家和包含 DVD、CD 和音频的列.所有的价格都在他们的行中.我需要写一个提示来做到这一点.

I have created an HTML table with many artists and columns with DVD's, CD's and Audio. All with prices in their rows. I need to write a prompt that will do this.

使用prompt() 方法,要求用户输入艺术家姓名、DVD 数量、CD 数量和用户希望购买的录音带数量.将答案保存在单独的变量中.还为每个值使用单独的提示.任何建议将不胜感激!

Using the prompt() method, ask the user to enter the artist's name, the number of DVD's, the number of CD's and the number of audio cassette's the user wishes to purchase. Save the answers in seperate variables. Also use a seperate prompt for each value. Any advice would be so appreciated!

来自下面评论的代码:

var w=window.prompt("please enter your name");
window.alert(w);
var x=widow.prompt ("Enter how many DVDs you want to buy");
window.alert(x);
var y=window.alert ("Enter how many CDs you want to buy");
window.alert(y);
var z=window.alert ("Enter how many Audio Cassettes you want to buy");
window.alert(z);

推荐答案

从听起来,以下内容满足您的要求:

From the sounds of it, the following meets your requirements:

var a, d, t;

while ( ! a ) a = prompt( "Which Artist?" );
while ( ! d ) d = prompt( "How many DVDs?" );
while ( ! t ) t = prompt( "How many tapes?" );

alert( "You want " + t + " Tapes, and " + d + " DVDs, of " + a + "." );

让我们把它分解一下,以便了解正在发生的事情:

Let's break it down you so have an understanding of what's going on:

var a, d, t;

在第一行,我声明了我计划在下面的代码中使用的各种变量.这是一种常见的做法,如果您想维护干净且易于管理的代码,这将是一个很好的习惯.

On the first line, I'm declaring the various variables I plan on using in the code below. This is a common practice, and would be a good habit to develop if you want to maintain clean and manageable code.

while ( ! a )

while 循环是一个循环,它会一遍遍地运行,直到满足某个条件.在这个例子中,只要我们没有 a 的值,我们就会告诉循环运行.接下来是我们尝试从用户那里收集 a 的值:

The while loop is a loop that will run over and over, until a condition is met. In this example, we're telling the loop to run as long as we don't have a value for a. What comes next is our attempt to collect a value of a from the user:

while ( ! a ) a = prompt( "Which Artist?" );

每次while循环运行时,我们都会提示用户回答问题.我们接受他们的答案,并将其分配给 a.如果他们什么也没输入,我们的 while 循环会再次运行,再次提示他们.此时您可能会理解接下来的两个 while 循环.

Each time the while loop runs, we will prompt the user to answer the question. We take their answer, and assign it to a. If they entered nothing, our while loop runs again, prompting them again. You can probably make sense of the next two while loops at this point.

最后是我们的警报,它收集各种值并将它们显示给用户:

Lastly is our alert, which gathers up the various values and shows them to the user:

alert( 'Artist ' + a );

这也提供了一个字符串连接的例子,或者将两个字符串连接在一起.我们有一个存储在 a 中的值,以及一个明确写成文本的值.我们使用 + 运算符将这两者连接在一起,就像用胶水将绳子的两端绑在一起一样.随着我们添加更多字符串和更多变量,我们越来越多地使用 + 运算符:

This also presents an example of string concatenation, or joining together of two strings. We have a value stored inside a, and a value written explicitly as text. We use the + operator to join both of these together, like glue tying two ends of a rope together. As we add more strings, and more variables, we use the + operator more and more:

alert( "You want " + t + " Tapes, and " + d + " DVDs, of " + a + "." );

运行此代码时,tda 将全部替换为最终用户插入的实际值.

When this code is ran, t, d, and a will all be replaced with the actual values inserted by the end-user.

请注意,这是您的作业要求的一个非常基本的实现.一个真正的解决方案是测试输入的类型以确保它是预期的格式.例如,当询问用户想要多少 DVD 时,您可能希望将可接受"的答案限制为仅整数.

Note, this is a very basic implementation of what your homework requires. A real solution would test the type of input to make sure it's of the expected format. For instance, when asking how many DVDs the user wants, you may want to restrict 'acceptable' answers to integers only.

祝你好运!

这篇关于JavaScript Prompt() 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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