【发布时间】: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 <= 3; i++) {注意花括号,您当然需要在 for 循环结束时使用另一个闭合花括号。 -
所以我在 tbody.appendChild(trElement);像这样? tbody = document.getElementsByTagName('tbody')[0]; for (var i = 0; i
标签: javascript loops prompt