我的提示框不起作用?自从我把元素放进去之后就不起作用了。
我需要提示框,以便我可以输入信息,然后该信息会自动进入表格,然后通过在表格的行中添加 * 来自动读取最大金额的 $。我并不是 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/