JavaScript Prompt() 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10215302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 09:04:18  来源:igfitidea点击:

JavaScript Prompt() method

javascriptprompt

提问by Mary Catherine Grassell

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.

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

For Example...

例如...

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.

我创建了一个 HTML 表格,其中包含许多艺术家和带有 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!

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

Edit: code from comment below:

编辑:来自下面评论的代码:

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

回答by Sampson

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 )

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 afrom the user:

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

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

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.

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

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

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

alert( 'Artist ' + 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:

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

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

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

运行此代码时td、 和a将全部替换为最终用户插入的实际值。

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.

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

Best of luck!

祝你好运!

回答by Bergi

Use a loop over the values object/array. Maybe use a second (nested) loop to prompt again until a value was entered.

在值对象/数组上使用循环。也许使用第二个(嵌套)循环再次提示,直到输入值。

回答by sarath galimelu

Use multiple functions such that on click of the first prompt the other function gets called which handles another prompt where u can save the variable value seperately.use it recursively.

使用多个函数,以便在单击第一个提示时调用另一个函数,该函数处理另一个提示,您可以在其中单独保存变量值。递归使用它。