2012-02-28 15 views
0

私は非常に新しいJavascriptでしかし、私は通貨コンバータを作成することを試みているhtml selectを使用して、素晴らしいことですが、関数を呼び出すと、ifステートメントをまっすぐにelse {}ステートメント通貨コンバータ! (Ifステートメント)

function convUSD() 
{ 
    RATE_GBP = 0.632111252; 
    RATE_EURO = 0.746435769; 
    RATE_AUD = 0.92945441; 

    if(document.selectBox.slBoxCurrency.selectedIndex == 0 && document.frmCurrencyC.radioUSD.checked) 
    { 
     window.alert("Sorry cant do USD to USD convertion! Please select another value."); 
    } 
    else if(document.selectBox.slBoxCurrency.selectedIndex == 0 && document.frmCurrencyC.radioGBP.checked) 
    { 
     inputBox = parseFloat(document.frmCurrencyC.textInputNum.value); 
     outPutBox = inputBox * RATE_GBP; 
     document.frmCurrencyC.textOutPutTotal.value = outPutBox; 
    } 
    else if(document.selectBox.slBoxCurrency.selectedIndex == 0 && document.frmCurrencyC.radioEURO.checked) 
    { 
     inputBox = parseFloat(document.frmCurrencyC.textInputNum.value); 
     outPutBox = inputBox * RATE_EURO; 
     document.frmCurrencyC.textOutPutTotal.value = outPutBox; 
    } 
    else if(document.selectBox.slBoxCurrency.selectedIndex == 0 && document.frmCurrencyC.radioAUD.checked) 
    { 
     inputBox = parseFloat(document.frmCurrencyC.textInputNum.value); 
     outPutBox = inputBox * RATE_AUD; 
     document.frmCurrencyC.textOutPutTotal.value = outPutBox; 
    } 
    else 
    { 
     window.alert("Whoops there was an error"); 
    } 
} 

最初、私は実際にそれがストレートelseステートメントに向かうGBPに例のUSDのために何をしたいとき文がしかし、正常に動作している場合。

もしあなたが何らかのエラーを発見した場合、あるいは何かがうまくいけばそれは大いに感謝します。

+2

をHTMLフォームこのコードリファレンスを投稿してください。できる場合は、http://jsfiddle.netに投稿してください –

+0

http://jsfiddle.net/X8MyF/完全なhtml/jsコード、ありがとうございます – JayJsNewbie

+1

ほとんどすべてのコードは冗長です。最初に、 'selectedIndex'をチェックしてください。非ゼロの場合は終了します。次に、チェックボックスがオンになっているコンバージョン率を把握します。テキストを解析し、変換レートを掛けて、それを吐き出す。 –

答えて

0

ドロップダウンリストから変換元の通貨を確認する代わりに、ラジオをチェックして、ドロップダウンリストの選択が "to"通貨と一致するように強制します。

function convUSD() { 
    ... 
    if (document.selectBox.slBoxCurrency.selectedIndex == 0 && 
     document.frmCurrencyC.radioGBP.checked) { 
     ... 
    } 
    // etc 
} 

コードを見ると、最初の機能では、「通貨」ではなく「通貨」を確認する必要があるようです。だから、calculateCC()で、代わりにラジオボタンのドロップダウンリスト、見て:

function calculateCC() { 
    if (document.selectBox.slBoxCurrency.selectedIndex == 0) { 
     convUSD(); 
    } 
    // etc 
} 

の作業のデモ:http://jsfiddle.net/X8MyF/2/

0

これ以外の場合は、ifを減らすことでこれを大幅に簡素化できます。実行するのが早くなり、最終的にはデバッグが容易になります。 このコードはあなたのバグをより容易に特定するのに役立ちますが、残りのコードとHTMLを見ることなく、元のコードが失敗している理由がわかりません。あなたはjsfiddle.netのテストサイトを設定してリンクを共有することができます。これにより、コード全体をテストしてデバッグすることができます。

まずあなたはラジオが選択されたを見て、あなたがに変換されている通貨を確認し:

function calculateCC() { 
    if (document.frmCurrencyC.radioUSD.checked) { 
     convUSD(); 
    } 
    // etc 
} 

それから私が期待するあなたのロジックが混ざっ持っ

function convUSD() 
{ 
    RATE_GBP = 0.632111252; 
    RATE_EURO = 0.746435769; 
    RATE_AUD = 0.92945441; 

    if (document.selectBox.slBoxCurrency.selectedIndex == 0) 
    { 
     if (document.frmCurrencyC.radioUSD.checked) { 
      window.alert("Sorry cant do USD to USD convertion! Please select another value."); 
     } 
     else if (document.frmCurrencyC.radioGBP.checked) { 
      inputBox = parseFloat(document.frmCurrencyC.textInputNum.value); 
      outPutBox = inputBox * RATE_GBP; 
      document.frmCurrencyC.textOutPutTotal.value = outPutBox; 
     } 
     else if (document.frmCurrencyC.radioEURO.checked){ 
      inputBox = parseFloat(document.frmCurrencyC.textInputNum.value); 
      outPutBox = inputBox * RATE_EURO; 
      document.frmCurrencyC.textOutPutTotal.value = outPutBox; 
     } 
     else if (document.frmCurrencyC.radioAUD.checked) { 
      inputBox = parseFloat(document.frmCurrencyC.textInputNum.value); 
      outPutBox = inputBox * RATE_AUD; 
      document.frmCurrencyC.textOutPutTotal.value = outPutBox; 
     } 
     else { 
      window.alert("Whoops there was an error"); 
     } 
    } 
    else { 
     alert("selected index != 0") 
    } 
} 
+0

ああ、私はあなたがすでにフィドルを投稿しているのを見ています。 –

+0

私はコーディングの標準について謝罪します、チュートリアルを見て、本能に行くことは私が行ってきた方法です!私はあなたが将来のラスCのためのIfの文をどのように単純化したかを指摘しました!残念ながら、私はまだそれが残りのステートメントをスキップする理由をまだ理解できません:/ – JayJsNewbie

+0

謝罪するものはありません。あなたの人生を楽にしてくれる少しの指導を提供しようとしています! –