货币换算! (如果声明)

时间:2012-02-28 00:53:14

标签: javascript

我在Javascript上相当新,但是我一直在尝试使用html select来创建一个货币转换器,但是在调用函数时,它似乎直接跳过if语句直接转到else {}语句

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

第一个If语句工作正常但是当我真的想要做例如USD到GBP时,它会直接转到else语句。

如果你们发现任何错误,或者只是发现任何错误,那将非常感激。

2 个答案:

答案 0 :(得分:0)

如果没有别的,你可以通过减少if来简化这一点。它运行起来会更快,最终更容易调试。 此代码也可以帮助您更容易地识别您的错误,虽然没有看到其他代码和HTML,但我看不出原始代码失败的充分理由。您可以尝试在jsfiddle.net上设置我们的测试站点并共享链接;这让我们可以完整地测试和调试你的代码。

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")
    }
}

答案 1 :(得分:0)

你的逻辑混乱了:

首先,通过查看选择的广播来检查您要转换的货币:

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

然后我希望您查看下拉列表以查看您要转换的货币,而是检查无线电,强制下拉列表选项与“到”货币匹配:

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

查看您的代码,似乎在您的第一个函数中,您需要检查“from”货币,而不是“to”货币。因此,在calculateCC()中,请查看下拉列表,而不是单选按钮:

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

工作演示: http://jsfiddle.net/X8MyF/2/

相关问题