javascript - 创建带有提示的网页

标签 javascript loops prompt

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

我需要提示框,以便我可以输入信息,然后该信息会自动进入表格,然后通过在表格的行中添加 * 来自动读取最大金额的 $。我并不是 100% 精通 js 编码,所以如果这是一个简单的修复,请不要笑(正是这些简单的事情让我着迷)。

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

“我的 table 在头部”

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

}

最佳答案

问题是您正在使用 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 = 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);

希望这有帮助! :)

关于javascript - 创建带有提示的网页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45991560/

相关文章:

javascript - Leaflet.markercluster onclick 错误 - 无法在 'appendChild' : parameter 1 is not of type 'Node' 上执行 'Node'

javascript - jQuery 验证 - 两个必填字段和一条错误消息

javascript - 罚款上传者删除链接

C++ - 如果在循环中声明一个对象,它的析构函数是否在循环结束时调用?

c - 提高 C 循环的效率

browser - 使用命令提示符打开网站并输入用户名和密码

c# 执行 shell 命令并获取结果

javascript - 如何使用chart.js在条形图中绘制多个颜色条

MySQL CURSOR 循环在存储过程中添加额外的传递

javascript - 尝试获取提示以将提示的前三个字符限制为仅字母