2011-05-11 73 views
-1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 
</head> 

<script language="javascript"> 


     //This is gobal array for currency and exchange rate which are used in this web page 
     var strCurrency=['Yen','US Dollar','Euro','Swiss Frank','Danish Korona']; 
     var dblExchangeRate=[128.7,1.59,1.15,2.06,9.69]; 

     //this function will allow the user to see if their currency covert 
     function Currency() 
     { 

      //collect information from text boxes and combo box 
      var txtSterling=document.getElementById("txtSterling") 
      var sleCurrency=document.getElementById("cmbCurrency") 
      var txtCovert=document.getElementById("txtCovert") 
      var cmbCurrency=document.getElementById("cmbCurrency") 

      //this will make sure text box is empty from the start 
      txtCovert.value=''; 

      //this will check to see when the user enter in numbers is vaild and there are no characters 
      if (isNaN(txtSterling.value)) 
      { 
       txtSterling.value=''; 
       alert('Please enter in numerical value only'); 
       txtSterling.focus(); 
       return; 

       //this will check the index of cmbCurrency and display the new currency coverstion in txtCovert. This is done by multiplying txtSterling by exchange rate for the new currency 
       var strSlectCurrency= cmbCurrency.selectedIndex; 
       var strCurrency= cmbCurrency.options[strSlectCurrency].text; 
       txtCovert.value= (txtSterling.value * dblExchangeRate[strSlectCurrency]).toFixed(2) + '' + strCurrency; 
      } 




     } 
</script> 

<body> 
<h1 align="center">Money Currency Converter</h1> 
<p align="left">&nbsp;</p> 
<p align="left">Please enter in the amount you wish to covert £ 
    <input type="text" name="txtSterling" id="txtSterling"/> 
</p> 
<p align="left">Please select a currency    
    <select name="cmbCurrency" id="cmbCurrency" onChange="Currency()"> 
    <option>Euro</option> 
    <option>US Dollar</option> 
    <option>Swiss Frank</option> 
    <option>Danish Korona</option> 
    <option>Yen</option> 
    </select> 
</p> 
<p align="left"> 
    <input type="button" name="cmdCurrency" id="cmdCurrency" value="Convert" onCr65trfg5trrfrfd87lick="Currency()" /> 
</p> 
<p align="left"> 
    <input type="text" name="txtCovert" id="txtCovert" /> 
</p> 
</body> 
</html> 

爲什麼沒有發生,當我點擊「轉換」,儘管事實上,我已經設置按鈕的事件處理程序?當我點擊「轉換」它不會做任何事情

回答

7

onCr65trfg5trrfrfd87lick應爲onclick

+0

要同意XHTML規範,不應該所有屬性都是小寫,即'onclick'嗎? – n00dle 2011-05-11 15:55:45

+0

是的!我通常只是使用jQuery綁定點擊或添加一個事件監聽器,所以忘了這一點。 – 2011-05-11 15:56:40

+0

是的。 [XHTML文檔必須使用小寫字母表示所有HTML元素和屬性名稱。](http://www.w3.org/TR/xhtml1/) – 2011-05-11 15:56:51

4

有幾件事情錯了,此代碼:

  • onCr65trfg5trrfrfd87lick應該讀onclick
  • if (isNaN(txtSterling.value))應該是if (isNaN(parseInt(txtSterling.value,10))),因爲前者會將空字符串(「」)變爲0,而不是NaN。未經此更改,您的錯誤處理塊將無法正常工作。還要注意,這將不允許用戶編寫像「1,000」這樣的複雜字符串。改善這一點是作爲練習給讀者的。
  • 您已將全部代碼放入空字符串處理塊中。即使這個塊被修復,你的代碼也會被破壞。
  • 您的數組strCurrencydblExchangeRate與下拉框中的貨幣順序不匹配,所以這些值是錯誤的。
  • 丹麥貨幣是克朗,而不是Korona(這是一種拼寫錯誤的酒精飲料)。
  • 您也在幾處拼錯「轉換」。

這裏的固定版本(雖然IMO它仍然是出這個問題的範圍的問題樣式):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 
</head> 

<script language="javascript"><!-- 
//This is gobal array for currency and exchange rate which are used in this web page 
var strCurrency=['Yen','US Dollar','Euro','Swiss Frank','Danish Krone']; 
var dblExchangeRate=[128.7,1.59,1.15,2.06,9.69]; 

//this function will allow the user to see if their currency covert 
function Currency() { 

    //collect information from text boxes and combo box 
    var txtSterling=document.getElementById("txtSterling") 
    var sleCurrency=document.getElementById("cmbCurrency") 
    var txtCovert=document.getElementById("txtCovert") 
    var cmbCurrency=document.getElementById("cmbCurrency") 

    //this will make sure text box is empty from the start 
    txtCovert.value=''; 

    //this will check to see when the user enter in numbers is vaild and there are no characters 
    if (isNaN(parseInt(txtSterling.value, 10))) { 
     txtSterling.value=''; 
     alert('Please enter in numerical value only'); 
     txtSterling.focus(); 
     return; 
    } 

    //this will check the index of cmbCurrency and display the new currency coverstion in txtCovert. This is done by multiplying txtSterling by exchange rate for the new currency 
    var strSlectCurrency= cmbCurrency.selectedIndex; 
    var strCurrency= cmbCurrency.options[strSlectCurrency].text; 
    txtCovert.value= (txtSterling.value * dblExchangeRate[strSlectCurrency]).toFixed(2) + ' ' + strCurrency; 
} 
//--></script> 

<body> 
<h1 align="center">Money Currency Converter</h1> 
<p align="left">&nbsp;</p> 
<p align="left">Please enter in the amount you wish to covert £ 
    <input type="text" name="txtSterling" id="txtSterling"/> 
</p> 
<p align="left">Please select a currency    
    <select name="cmbCurrency" id="cmbCurrency" onChange="Currency()"> 
    <option>Yen</option> 
    <option>US Dollar</option> 
    <option>Euro</option> 
    <option>Swiss Frank</option> 
    <option>Danish Krone</option> 
    </select> 
</p> 
<p align="left"> 
    <input type="button" name="cmdCurrency" id="cmdCurrency" value="Convert" onClick="Currency()" /> 
</p> 
<p align="left"> 
    <input type="text" name="txtCovert" id="txtCovert" /> 
</p> 
</body> 
</html> 

看到它的工作here

希望這會有所幫助。

0

您的if塊是錯誤的。它應該是

if (isNaN(txtSterling.value)) 
{ 
    txtSterling.value=''; 
    alert('Please enter in numerical value only'); 
    txtSterling.focus(); 
    return; 
} 
//this will check the index of cmbCurrency and display the new currency coverstion in txtCovert. This is done by multiplying txtSterling by exchange rate for the new currency 
var strSlectCurrency= cmbCurrency.selectedIndex; 
var strCurrency= cmbCurrency.options[strSlectCurrency].text; 
txtCovert.value= (txtSterling.value * dblExchangeRate[strSlectCurrency]).toFixed(2) + '' + strCurrency; 
+2

是的(雖然'else'是多餘的)。 – 2011-05-11 16:22:11