【问题标题】:Create a web page with prompts创建带有提示的网页
【发布时间】:2017-08-31 23:48:25
【问题描述】:

我的提示框不起作用?自从我把元素的东西放进去之后,那里就不起作用了。

我需要提示框,以便我可以输入信息,然后该信息自动进入表格,然后通过在表格的行中有 * 来自动读取最大金额的 $。我不是 100% 的 js 编码,所以如果它是一个简单的解决方法,请不要笑(这是让我感兴趣的简单事情)。

这是我的代码,我不知道我做错了什么

“我的桌子在头上”

function money(){

this.currency = "";
this.amount = "";
this.exchangeRate = "";
this.ausDollars = "";

tbody = document.getElementsByTagName('tbody')[0];
for (var i = 0; i <= 3; i++) 
  trElement = document.createElement('tr');
  tbody.appendChild(trElement);

// Read the 3 letter currency abbreviation entered
  currency = prompt('Please enter a 3-letter currency abbreviation', +i, "");
  // If the input data is invalid ask the user to re-enter it
  while (currency.length != 3) {
    currency = prompt('Currency abbreviation was not entered', "");
    currency = parseFloat(currency);
  }

currencyTH = document.createElement('th');
  currencyText = document.createTextNode(currency);
  currencyTH.appendChild(currencyText);
  trElement.appendChild(currencyTH);

 // Read the amount and convert it to entered currency 
  amount = prompt('Please enter an amount of money in that currency', +i, "");
  // If the input data is invalid ask the user to re-enter it
  while (isNaN(amount) || amount < 0) {
    amount = prompt('An amount of money was not entered')
    amount = parseFloat(amount);
  }

amountTH = document.createElement('th');
  amountText = document.createTextNode(amount);
  amountTH.appendChild(amountText);
  trElement.appendChild(amountTH);

 exchangeRateTH = document.createElement('th');
  exchangeRateText = document.createTextNode(exchangeRate);
  exchangeRateTH.appendChild(exchangeRateText);
  trElement.appendChild(exchangeRateTH);

}

【问题讨论】:

  • 您有一个 for 循环,它创建了 trElement 4 次。但只对最后一个做一些事情。我假设您的 for 循环希望更像 for (var i = 0; i &lt;= 3; i++) { 注意花括号,您当然需要在 for 循环结束时使用另一个闭合花括号。
  • 所以我在 tbody.appendChild(trElement);像这样? tbody = document.getElementsByTagName('tbody')[0]; for (var i = 0; i

标签: javascript loops prompt


【解决方案1】:

问题在于您使用 prompt() 来提示用户输入显示与用户输入相关的错误消息。为此,您正在寻找 alert()

首先您在提示中设置货币:

currency = prompt('Please enter a 3-letter currency abbreviation', +i, "");

然后你运行:

 currency = prompt('Currency abbreviation was not entered', "");

覆盖最初存储在currency 中的值,因此您无法在下一行对其运行parseFloat()

 currency = parseFloat(currency);

要解决此问题,请使用:

alert('Currency abbreviation was not entered');

请注意,amount 也是如此。而不是:

amount = prompt('An amount of money was not entered');

用途:

alert('An amount of money was not entered');

另请注意,您的while 循环结构在满足正确值之前无限期提示略有错误。您应该在循环之外设置一个变量,然后检查条件,而不是最初提示然后运行您的 while 循环。而parseFloat() 必须while循环之外,否则你会陷入无限循环!

var currency = '';
currency = prompt("Please enter 3 characters");
while (currency.length != 3) {
  alert('Currency must be three characters');
  currency = prompt("Please enter 3 characters");
}
currency = parseFloat(currency);

console.log("The stored value was: " + currency);

希望这会有所帮助! :)

【讨论】:

  • 您好,请问金额和汇率也可以吗?
  • @Claire -- 是的,您需要这样做才能使提示正常运行 =]
  • 喜欢吗?变量金额 = ''; while (isNaN(amount) || amount
  • 关闭;事实证明,您实际上需要在while 中再次调用警报prompt()。我已经更新了我的答案来展示这个 =]
  • 分而治之以隔离您的问题。他们正在我的示例中工作,因此请以此为基础构建。
猜你喜欢
  • 1970-01-01
  • 2016-01-25
  • 1970-01-01
  • 1970-01-01
  • 2011-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多