当我点击“转换”它不会做任何事情

时间:2011-05-11 15:52:06

标签: javascript

<!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>

为什么在点击“转换”时没有任何反应,尽管我已在按钮上设置了事件处理程序?

3 个答案:

答案 0 :(得分:7)

onCr65trfg5trrfrfd87lick应该阅读onclick

答案 1 :(得分:4)

此代码有几个问题:

  • onCr65trfg5trrfrfd87lick应阅读onclick
  • if (isNaN(txtSterling.value))应为if (isNaN(parseInt(txtSterling.value,10))),因为前者将空字符串(“”)与0结合,而不是NaN。如果没有此更改,您的错误处理块将无法工作。另请注意,这不允许用户编写复杂的字符串,如“1,000”。改善这一点是留给读者的练习。
  • 您已将所有代码放入空字符串处理块中。即使修复了这个块,你的代码也会被破坏。
  • 您的数组strCurrencydblExchangeRate与下拉框中的货币顺序不匹配,因此值不正确。
  • 丹麦货币是Krone,而不是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

希望这有帮助。

答案 2 :(得分: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;