When I click on "convert" it won't do anything
<!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" />
<t开发者_如何学Pythonitle>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"> </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>
Why does nothing happen when I click "Convert", despite the fact that I have set an event handler on the button?
onCr65trfg5trrfrfd87lick
should read onclick
.
There are several things wrong with this code:
onCr65trfg5trrfrfd87lick
should readonclick
.if (isNaN(txtSterling.value))
should beif (isNaN(parseInt(txtSterling.value,10)))
, since the former co-erces the empty string ("") to 0, which is not NaN. Your error handling block won't work without this change. Note also that this will not allow the user to write complex strings like "1,000". Improving this is left as an exercise to the reader.- You've put all your code into the empty string handling block. Even once this block is fixed, your code will be broken.
- Your arrays
strCurrency
anddblExchangeRate
do not match the order of currencies in the dropdown box, so the values are wrong. - The Danish currency is the Krone, not the Korona (which is a misspelt alcoholic beverage).
- You've also misspelt "convert" in a few places.
Here's the fixed version (though IMO it still has style problems that are out of the scope of this question):
<!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"> </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>
See it working here.
Hope this helps.
Your if
block is wrong. It should be
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;
精彩评论