货币转换器!(If语句)



我是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语句。

如果你们发现了任何错误,或者做得很好,我们将不胜感激。

您的逻辑混淆了:

首先,您可以通过查看选择的电台来查看您要转换为哪种货币:

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/

如果没有其他内容,您可以通过减少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")
    }
}

最新更新