2017-08-31 113 views
0

我的提示框不起作用?自從我把元素的東西放在那裏不工作。用提示創建一個網頁

我需要提示框,以便我可以輸入信息,然後該信息自動進入表格,然後通過在表格的行中自動讀取最大金額$。我不是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); 

} 
+0

你有一個for循環創建trElement 4次。但只做最後一件事。我假設你的for循環想要更像'for(var i = 0; i <= 3; i ++){'注意大括號,你當然需要在for循環結尾使用另一個大括號。 – Keith

+0

因此,我有tbody.appendChild(trElement)後的大括號;喜歡這個? tbody = document.getElementsByTagName('tbody')[0]; (var i = 0; i <= 3; i ++){ \t trElement = document。的createElement( 'TR'); \t tbody.appendChild(trElement); \t} – Claire

回答

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

希望這有助於! :)

+0

嗨,我可以這樣做的金額和交換以及? – Claire

+0

@克萊爾 - 是的,你需要爲了使該提示功能正常=] –

+0

像這樣? var amount =''; \t while(isNaN(amount)|| amount <0){ alert('金額未輸入') } amount = parseFloat(amount); console.log(「存儲的值是:」+金額); var exchangeRate =''; (isNaN(exchangeRate)|| exchangeRate <0){ alert('Exchange rate was not entered',「」); } \t exchangeRate = parseFloat(exchangeRate); console.log(「存儲的值是:」+ exchangeRate); – Claire